Java Reference
In-Depth Information
operation can be very suitable for this purpose. Sometimes you want to go further than
re-
duce
allows, though.
Enter the
collector
, a general-purpose construct for producing complex values from streams.
These can be used with any
Stream
by passing them into the
collect
method.
The standard library provides a bunch of useful collectors out of the box, so let's look at
those first. In the code examples throughout this chapter the collectors are statically imported
from the
java.util.stream.Collectors
class.
Into Other Collections
Some collectors just build up other collections. You've already seen the
toList
collector,
which produces
java.util.List
instances. There's also a
toSet
collector and a
toCollec-
tion
collector, which produce instances of
Set
and
Collection
. I've talked a lot so far
about chaining
Stream
operations, but there are still times when you'll want to produce a
Collection
as a final value—for example:
▪ When passing your collection to existing code that is written to use collections
▪ When creating a final value at the end of a chain of collections
▪ When writing test case asserts that operate on a concrete collection
Normally when we create a collection, we specify the concrete type of the collection by call-
ing the appropriate constructor:
List
<
Artist
>
artists
=
new
new
ArrayList
<>();
But when you're calling
toList
or
toSet
, you don't get to specify the concrete implementa-
tion of the
List
or
Set
. Under the hood, the streams library is picking an appropriate imple-
mentation for you. Later in this topic I'll talk about how you can use the streams library to
perform data parallel operations; collecting the results of parallel operations can require a
different type of
Set
to be produced than if there were no requirement for thread safety.
It might be the case that you wish to
collect
your values into a
Collection
of a specific
type if you require that type later. For example, perhaps you want to use a
TreeSet
instead
of allowing the framework to determine what type of
Set
implementation you get. You can