Java Reference
In-Depth Information
•For
String
s, the natural order is case sensitive, which means that
"Z"
is less than
"a"
. Passing the
predefined
Comparator
String.CASE_INSENSITIVE_ORDER
performs a case-insensitive sort.
Section 17.5.3 Filtering
String
s Then Sorting Them in Case-Insensitive Descending
Order
• Functional interface
Comparator
's
default
method
reversed
(p. 748) reverses an existing
Com-
parator
's ordering.
Section 17.6.1 Creating and Displaying a
List<Employee>
• When the instance method reference
System.out::println
is passed to
Stream
method
forEach
,
it's converted by the compiler into an object that implements the
Consumer
functional interface.
This interface's
accept
method receives one argument and returns
void
. In this case, the
accept
method passes the argument to the
System.out
object's
println
instance method.
Section 17.6.2 Filtering
Employee
s with Salaries in a Specified Range
• To reuse a lambda, you can assign it to a variable of the appropriate functional interface type.
•The
Comparator
interface's
static
method
comparing
(p. 753) receives a
Function
that's used to
extract a value from an object in the stream for use in comparisons and returns a
Comparator
object.
• A nice performance feature of lazy evaluation is the ability to perform short circuit evaluation—
that is, to stop processing the stream pipeline as soon as the desired result is available.
•
Stream
method
findFirst
is a short-circuiting terminal operation that processes the stream pipe-
line and terminates processing as soon as the first object from the stream pipeline is found. The
method returns an
Optional
containing the object that was found, if any.
Section 17.6.3 Sorting
Employee
s By Multiple Fields
• To sort objects by two fields, you create a
Comparator
that uses two
Function
s. First you call
Com-
parator
method
comparing
to create a
Comparator
with the first
Function
. On the resulting
Com-
parator
, you call method
thenComparing
with the second
Function
. The resulting
Comparator
compares objects using the first
Function
then, for objects that are equal, compares them by the
second
Function
.
Section 17.6.4 Mapping
Employee
s to Unique Last Name
String
s
• You can map objects in a stream to different types to produce another stream with the same num-
ber of elements as the original stream.
•
Stream
method
distinct
(p. 754) eliminates duplicate objects in a stream.
Section 17.6.5 Grouping
Employee
s By Department
•
Collectors
static
method
groupingBy
(p. 755) with one argument receives a
Function
that clas-
sifies objects in the stream—the values returned by this function are used as the keys in a
Map
. The
corresponding values, by default, are
List
s containing the stream elements in a given category.
•
Map
method
forEach
performs an operation on each key-value pair. The method receives an ob-
ject that implements functional interface
BiConsumer
. This interface's
accept
method has two
parameters. For
Map
s, the first represents the key and the second the corresponding value.
Section 17.6.6 Counting the Number of
Employee
s in Each Department
•
Collectors
static
method
groupingBy
with two arguments receives a
Function
that classifies
the objects in the stream and another
Collector
(known as the downstream
Collector
; p. 756).
•
Collectors
static
method
counting
returns a
Collector
that counts the number of objects in
a given classification, rather than collecting them into a
List
.