Java Reference
In-Depth Information
beginning to end in a single thread of execution. To perform the preceding task, you also
create two variables (
sum
and
counter
) that are
mutated
repeatedly—that is, their values
change—while the task is performed. You performed many similar array and collection
tasks, such as displaying the elements of an array, summarizing the faces of a die that was
rolled 6,000,000 times, calculating the average of an array's elements and more.
External Iteration Is Error Prone
Most Java programmers are comfortable with external iteration. However, there are several
opportunities for error. For example, you could initialize variable
sum
incorrectly, initialize
control variable
counter
incorrectly, use the wrong loop-continuation condition, increment
control variable
counter
incorrectly or incorrectly add each value in the array to the
sum
.
Internal Iteration
In
functional programming
, you specify
what
you want to accomplish in a task, but
not
how
to accomplish it. As you'll see in this chapter, to sum a numeric data source's elements
(such as those in an array or collection), you can use new Java SE 8 library capabilities that
allow you to say, “Here's a data source, give me the sum of its elements.” You do
not
need
to specify
how
to iterate through the elements or declare and use
any
mutable variables.
This is known as
internal iteration
, because the
library
determines how to access all the
elements to perform the task. With internal iteration, you can easily tell the library that
you want to perform this task with
parallel processing
to take advantage of your computer's
multi-core architecture—this can significantly improve the task's performance. As you'll
learn in Chapter 23, it's hard to create parallel tasks that operate correctly if those tasks
modify a program's state information (that is, its variable values). So the functional pro-
gramming capabilities that you'll learn here focus on
immutability
—not modifying the
data source being processed or any other program state.
Section 10.10 introduced Java SE 8's new interface features—
default
methods and
static
methods—and discussed the concept of a
functional interface
—an interface that
contains exactly one
abstract
method (and may also contain
default
and
static
meth-
ods). Such interfaces are also known as
single abstract method
(SAM) interfaces. Functional
interfaces are used extensively in functional programming, because they act as an object-
oriented model for a function.
Functional Interfaces in Package
java.util.function
Package
java.util.function
contains several functional interfaces. Figure 17.2 shows
the six basic generic functional interfaces. Throughout the table,
T
and
R
are generic type
names that represent the type of the object on which the functional interface operates and
the return type of a method, respectively. There are many other functional interfaces in
package
java.util.function
that are specialized versions of those in Fig. 17.2. Most are
for use with
int
,
long
and
double
primitive values, but there are also generic customiza-
tions of
Consumer
,
Function
and
Predicate
for binary operations—that is, methods that
take two arguments.