Collectors Flashcards

1
Q

Static Imports

A

Typically, we can find all the predefined implementations in the Collectors class.
import static java.util.stream.Collectors.*;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Collectors.toCollection()

A

When using the toSet() and toList() collectors, we can’t make any assumptions about their implementations, ie with toList() we cannot assume that we will get a LinkedList implementation
List<String> result = givenList.stream()
.collect(toCollection(LinkedList::new))
In above, result is collected to a LinkedList implementation</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Collectors.toList()

A

Collect all Stream elements into a List instance. The important thing to remember is that we can’t assume any particular List implementation with this method. If we want to have more control over this, we can use toCollection() instead.

List<String> result = givenList.stream()
.collect(toList());</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Collectors.toUnmodifiableList()

A

List<String> result = givenList.stream()
.collect(toUnmodifiableList());
if we try to modify result we will get an exception
assertThatThrownBy(() -> result.add("foo")) .isInstanceOf(UnsupportedOperationException.class);</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Collectors.toSet()

A

Set<String> result = givenList.stream()
.collect(toSet());</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Collectors.toUnmodifiableSet()

A

Set<String> result = givenList.stream()
.collect(toUnmodifiableSet());</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Collectors.toMap()

A

The toMap() collector can be used to collect Stream elements into a Map instance. To do so, we need to provide two functions: keyMapper() and valueMapper().

keyMapper() to extract a Map key from a Stream element. we can use valueMapper() to extract a value associated with a given key
Map<String, Integer> result = givenList.stream()
.collect(toMap(Function.identity(), String::length))
Function.identity() is just a shortcut for defining a function that accepts and returns the same value

If it sees duplicate keys, it immediately throws an IllegalStateException.

In such cases with key collision, we should use toMap() with another signature

Map<String, Integer> result = givenList.stream()
.collect(toMap(Function.identity(), String::length, (item, identicalItem) -> item));

The third argument here is a BinaryOperator(), where we can specify how we want to handle collisions. In this case, we’ll just pick any of these two colliding values because we know that the same strings will always have the same lengths too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Collectors.toUnmodifiableMap()

A

Map<String, Integer> result = givenList.stream() .collect(toUnmodifiableMap(Function.identity(), String::length))
assertThatThrownBy(() -> result.put(“foo”, 3)) .isInstanceOf(UnsupportedOperationException.class);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Collectors.collectingAndThen()

A

CollectingAndThen() is a special collector that allows us to perform another action on a result straight after collecting ends.
Let’s collect Stream elements to a List instance, and then convert the result into an ImmutableList instance:
List<String> result = givenList.stream()
.collect(collectingAndThen(toList(), ImmutableList::copyOf))</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Collectors.joining()

A

The Joining() collector can be used for joining Stream<String> elements.
String result = givenList.stream() .collect(joining());
or
String result = givenList.stream()
.collect(joining(" "));</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Collectors.counting()

A

Counting() is a simple collector that allows for the counting of all Stream elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Collectors.summarizingDouble/Long/Int()

A

SummarizingDouble/Long/Int is a collector that returns a special class containing statistical information about numerical data in a Stream of extracted elements.
DoubleSummaryStatistics result = givenList.stream()
.collect(summarizingDouble(String::length));

assertThat(result.getAverage()).isEqualTo(2);
assertThat(result.getCount()).isEqualTo(4);
assertThat(result.getMax()).isEqualTo(3);
assertThat(result.getMin()).isEqualTo(1);
assertThat(result.getSum()).isEqualTo(8);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Collectors.averagingDouble/Long/Int()

A

Double result = givenList.stream()
.collect(averagingDouble(String::length));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Collectors.summingDouble/Long/Int()

A

SummingDouble/Long/Int is a collector that simply returns a sum of extracted elements
Double result = givenList.stream()
.collect(summingDouble(String::length));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Collectors.maxBy/minBy

A

MaxBy() and MinBy() collectors return the biggest/smallest element of a Stream according to a provided Comparator instance.
Optional<String> result = givenList.stream()
.collect(maxBy(Comparator.naturalOrder()));</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Collectors.groupingBy()

A

Typically, we can use the GroupingBy() collector to group objects by a given property and then store the results in a Map instance.
Map<Integer, Set<String>> result = givenList.stream()
.collect(groupingBy(String::length, toSet()));</String>

17
Q

Collectors.partitioningBy()

A

partitioningBy() is a specialized case of groupingBy() that accepts a Predicate instance, and then collects Stream elements into a Map instance that stores Boolean values as keys and collections as values. Under the “true” key, we can find a collection of elements matching the given Predicate, and under the “false” key, we can find a collection of elements not matching the given Predicate.

Map<Boolean, List<String>> result = givenList.stream()
.collect(partitioningBy(s -> s.length() > 2))</String>

{false=[“a”, “bb”, “dd”], true=[“ccc”]}