Java Reference
In-Depth Information
• When the parameter list contains only one parameter, the parentheses may be omitted, as in:
value -> System.out.printf( "%d " , value)
• A lambda with an empty parameter list is defined with () to the left of the arrow token ( -> ), as in:
() -> System.out.println( "Welcome to lambdas!" )
• There are also specialized shorthand forms of lambdas that are known as method references.
Section 17.2.3 Streams
• Streams (p. 734) are objects that implement interface Stream (from the package java.util.stream )
and enable you to perform functional programming tasks. There are also specialized stream inter-
faces for processing int , long or double values.
• Streams move elements through a sequence of processing steps—known as a stream pipeline—
that begins with a data source, performs various intermediate operations on the data source's ele-
ments and ends with a terminal operation. A stream pipeline is formed by chaining method calls.
• Unlike collections, streams do not have their own storage—once a stream is processed, it cannot
be reused, because it does not maintain a copy of the original data source.
• An intermediate operation (p. 734) specifies tasks to perform on the stream's elements and al-
ways results in a new stream.
• Intermediate operations are lazy (p. 734)—they aren't performed until a terminal operation is
invoked. This allows library developers to optimize stream-processing performance.
• A terminal operation (p. 734) initiates processing of a stream pipeline's intermediate operations
and produces a result. Terminal operations are eager (p. 734)—they perform the requested op-
eration when they are called.
Section 17.3 IntStream Operations
•An IntStream (package java.util.stream ) is a specialized stream for manipulating int values.
Section 17.3.1 Creating an IntStream and Displaying Its Values with the forEach
Terminal Operation
IntStream static method of (p. 738) receives an int array as an argument and returns an Int-
Stream for processing the array's values.
IntStream method forEach (a terminal operation; p. 738) receives as its argument an object that
implements the IntConsumer functional interface (package java.util.function ). This inter-
face's accept method receives one int value and performs a task with it.
• The Java compiler can infer the types of a lambda's parameters and the type returned by a lambda
from the context in which the lambda is used. This is determined by the lambda's target type
(p. 738)—the functional interface type that is expected where the lambda appears in the code.
• Lambdas may use final local variables or effectively final (p. 738) local variables.
• A lambda that refers to a local variable in the enclosing lexical scope is known as a capturing lambda.
• A lambda can use the outer class's this reference without qualifying it with the outer class's name.
• The parameter names and variable names that you use in lambdas cannot be the same as any oth-
er local variables in the lambda's lexical scope; otherwise, a compilation error occurs.
Section 17.3.2 Terminal Operations count , min , max , sum and average
• Class IntStream provides terminal operations for common stream reductions— count (p. 739)
returns the number of elements, min returns the smallest int , max returns the largest int , sum re-
turns the sum of all the int s and average returns an OptionalDouble (package java.util ) con-
taining the average of the int s as a value of type double .
Search WWH ::




Custom Search