09 Lambdas
09 Lambdas
Note: the source for creating the slides for this lecture is chapter 17 in the book:
Java How to Program, Early Objects, 11 edition, Deitel and Deitel
filter Results in a stream containing only the elements that satisfy a condition.
distinct Results in a stream containing only the unique elements.
limit Results in a stream with the specified number of elements from the beginning
of the original stream.
map Results in a stream in which each element of the original stream is mapped to
a new value (possibly of a different type)—e.g., mapping numeric values to
the squares of the numeric values. The new stream has the same number of
elements as the original stream.
sorted Results in a stream in which the elements are in sorted order. The new stream
has the same number of elements as the original stream.
forEach Performs processing on every element in a stream (e.g., display each element).
Reduction operations—Take all values in the stream and return a single value
elements as the original stream.
sorted Results in a stream in which the elements are in sorted order. The new stream
has the same number of elements as the original stream.
forEach Performs processing on every element in a stream (e.g., display each element).
Reduction operations—Take all values in the stream and return a single value
average Calculates the average of the elements in a numeric stream. Terminal Stream
count
max
Returns the number of elements in the stream.
Locates the largest value in a numeric stream. Operations
min Locates the smallest value in a numeric stream.
reduce Reduces the elements of a collection to a single value using an associative accumu-
lation function (e.g., a lambda that adds two elements).
Mutable reduction operations—Create a container (such as a collection or StringBuilder)
collect Creates a new collection of elements containing the results of the stream’s prior
operations.
toArray Creates an array containing the results of the stream’s prior operations.
Search operations
findFirst Finds the first stream element based on the prior intermediate operations; immedi-
ately terminates processing of the stream pipeline once such an element is found.
findAny Finds any stream element based on the prior intermediate operations; immediately
terminates processing of the stream pipeline once such an element is found.
anyMatch Determines whether any stream elements match a specified condition; immedi-
ately terminates processing of the stream pipeline if an element matches.
allMatch Determines whether all of the elements in the stream match a specified condition.
Fig. 17.4 | Common terminal Stream operations. © All rights reserved. Source: Java How to Program, Early Objects, 11 edition, Deitel and Deitel
FIGURE 30.1 The Stream class defines the aggregate operations for the elements in a stream.
The methods are invoked using a stream pipeline. A stream pipeline consists of a source (e.g., stream pipeline
a list, a set, or an array), a method that creates a stream, zero or more intermediate methods,
and a final terminal method. The following is a stream pipeline example:
© All rights reserved. Source: Introduction to Java Programming, 10 edition, Y. Daniel Liang
// output
content: 33 20 82 15 43 27
count is 6
max is 82
min is 15
sum is 220
average is 36.666666666666664
sum via reduce is 220
sum of squares is 11016
applying a filter: 20 15 27
// output
converting arr1 to integers:
13 32 36 94 15
converting arr3 to doubles:
1.1 2.2 3.3 4.4 5.5
printing the length of each word in arr3:
10 2 4 9 8
// output
strings: [java, compilers, c++, Discrete, compilers]
strings in uppercase: [JAVA, COMPILERS, C++, DISCRETE, COMPILERS]
strings in sorted order: [c++, compilers, compilers, Discrete, java]
strings in reverse sorted order: [java, Discrete, compilers, compilers, c++]
© All rights reserved.
Example 4
// output
[10, 13, 18, 14, 10, 15, 15, 18, 19, 13]
10 10 13 13 14 15 15
6
3.1622776601683795 3.605551275463989 4.242640687119285 3.7416573867739413 ……
© All rights reserved.
Classroom Exercise
• In a single line, use lambdas and streams to perform the below three tasks