Java Reference
In-Depth Information
Method filter is a stateless intermediate operation —it does not require any infor-
mation about other elements in the stream in order to test whether the current element
satisfies the predicate. Similarly method map (discussed shortly) is a stateless intermediate
operation. Method sorted is a stateful intermediate operation that requires information
about all of the other elements in the stream in order to sort them. Similarly method dis-
tinct is a stateful intermediate operation. The online documentation for each interme-
diate stream operation specifies whether it is a stateless or stateful operation.
Other Methods of the IntPredicate Functional Interface
Interface IntPredicate also contains three default methods:
and —performs a logical AND with short-circuit evaluation (Section 5.9) between
the IntPredicate on which it's called and the IntPredicate it receives as an ar-
gument.
negate reverses the boolean value of the IntPredicate on which it's called.
or —performs a logical OR with short-circuit evaluation between the IntPredi-
cate on which it's called and the IntPredicate it receives as an argument.
Composing Lambda Expressions
You can use these methods and IntPredicate objects to compose more complex condi-
tions. For example, consider the following two IntPredicate s:
IntPredicate even = value -> value % 2 == 0 ;
IntPredicate greaterThan5 = value -> value > 5 ;
To locate all the even integers greater than 5, you could replace the lambda in line 46 with
the IntPredicate
even.and(greaterThan5)
17.3.5 Intermediate Operation: Mapping
Lines 54-58 of Fig. 17.5 create a stream pipeline that locates the odd integers in an Int-
Stream , multiplies each odd integer by 10, sorts the values in ascending order and displays
each value followed by a space.
Intermediate Operation map
The new feature here is the mapping operation that takes each value and multiplies it by
10. Mapping is an intermediate operation that transforms a stream's elements to new values
and produces a stream containing the resulting elements. Sometimes these are of different
types from the original stream's elements.
IntStream method map (line 56) receives an object that implements the IntUnary-
Operator functional interface (package java.util.function ). The lambda in line 55:
value -> value * 10
implements the interface's applyAsInt method, which receives an int and maps it to a
new int value. Calls to map are lazy . Method map is a stateless stream operation.
 
 
Search WWH ::




Custom Search