0% found this document useful (0 votes)
64 views9 pages

2.9 Collecting Results: Object Array

This document discusses various ways to collect the results of a stream into collections or other data structures in Java. It explains that toArray can be used to collect results into an array, while collect allows collecting results into collections like HashSet. Collect provides options to specify how to create, add, and combine elements into the target collection. Common collectors like toList, toSet and joining are available in the Collectors utility class.

Uploaded by

Bui Ngoc Khang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views9 pages

2.9 Collecting Results: Object Array

This document discusses various ways to collect the results of a stream into collections or other data structures in Java. It explains that toArray can be used to collect results into an array, while collect allows collecting results into collections like HashSet. Collect provides options to specify how to create, add, and combine elements into the target collection. Common collectors like toList, toSet and joining are available in the Collectors utility class.

Uploaded by

Bui Ngoc Khang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

2.

9 Collecting Results
• When you are done with a stream, you often just want
to look at the results instead of reducing them to a
value.
• You can call toArray and get an array of the stream
elements.
• Since it is not possible to create a generic array at
runtime, the expression stream.toArray() returns an
Object[] array.
• If you want an array of the correct type, pass in the
array constructor:
• String[] result = words.toArray(String[]::new); //
words.toArray() has type Object[]
2.9 Collecting Results

• Now suppose you want to collect the results in


a HashSet.
• If the collection is parallelized, you can’t put
the elements directly into a single HashSet
because a HashSet object is not threadsafe.
2.9 Collecting Results
• For that reason, you can’t use reduce.
• Each segment needs to start out with its own empty
hash set, and reduce only lets you supply one identity
value.
• Instead, use collect.
• It takes three arguments:
1. A supplier to make new instances of the target object, for
example, a constructor for a hash set.
2. An accumulator that adds an element to the target, for
example, an add method.
3. An combiner that merges two objects into one, such as
addAll .
2.9 Collecting Results
• NOTE: The target object need not be a
collection.
• It could be a StringBuilder or an object that
tracks a count and a sum.

• Here is how the collect method works for a


hash set:
HashSet result = stream.collect(HashSet::new,
HashSet::add, HashSet::addAll);
2.9 Collecting Results
• In practice, you don’t have to do that because
there is a convenient Collector interface for
these three functions, and a Collectors class
with factory methods for common collectors.
• To collect a stream into a list or set, you can
simply call :
List result = stream.collect(Collectors.toList());
Or
Set result = stream.collect(Collectors.toSet());
2.9 Collecting Results
• If you want to control which kind of set you
get, use the following call instead:
TreeSet result =
stream.collect(Collectors.toCollection(T
reeSet::new));
• Suppose you want to collect all strings in a
stream by concatenating them. You can call :
String result =
stream.collect(Collectors.joining());
2.9 Collecting Results
• If you want a delimiter between elements,
pass it to the joining method:
String result = stream.collect(Collectors.joining(",
"));
• If your stream contains objects other than
strings, you need to first convert them to
strings, like this:
String result =
stream.map(Object::toString).collect(Collectors.joining("
, "));
2.9 Collecting Results
• If you want to reduce the stream results to a sum,
average, maximum, or minimum, then use one of the
methods summarizing(Int|Long|Double).
• These methods take a function that maps the stream
objects to a number and yield a result of type
(Int|Long|Double)SummaryStatistics, with methods for
obtaining the sum, average, maximum, and minumum.
IntSummaryStatistics summary = words.collect(
Collectors.summarizingInt(String::length));
double averageWordLength =
summary.getAverage();
double maxWordLength = summary.getMax();
2.9 Collecting Results
• NOTE: So far, you have seen how to reduce or
collect stream values.
• But perhaps you just want to print them or
put them in a database.
• Then you can use the forEach method:
stream.forEach(System.out::println);

You might also like