Questions tagged [collectors]

Collector, as of Java 8, is a mutating reduction operation on a Stream. Use this tag for specific questions about the usage of Collectors.

Collector, as of Java 8, is an interface representing a mutating reduction operation on Stream, defined in the documentation as

A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed.

Sample usage:

The Collectors utility class provides several built-in methods to obtain instances of Collector:

  • To collect a Stream into a List, use Collectors.toList.
  • To collect a Stream into a Set, use Collectors.toSet.
  • To collect a Stream into a Map, use Collectors.toMap.

See also:

716 questions
530
votes
13 answers

NullPointerException in Collectors.toMap with null entry values

Collectors.toMap throws a NullPointerException if one of the values is null. I don't understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason why values cannot be null for…
Jasper
  • 6,076
  • 2
  • 14
  • 24
280
votes
10 answers

Java8: HashMap to HashMap using Stream / Map-Reduce / Collector

I know how to "transform" a simple Java List from Y -> Z, i.e.: List x; List y = x.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList()); Now I'd like to do basically the same with a Map,…
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
212
votes
8 answers

How to add elements of a Java8 stream into an existing List

Javadoc of Collector shows how to collect elements of a stream into a new List. Is there an one-liner that adds the results into an existing ArrayList?
codefx
  • 9,872
  • 16
  • 53
  • 81
139
votes
1 answer

Is there a Collector that collects to an order-preserving Set?

Collectors.toSet() does not preserve order. I could use Lists instead, but I want to indicate that the resulting collection does not allow element duplication, which is exactly what Set interface is for.
gvlasov
  • 18,638
  • 21
  • 74
  • 110
107
votes
1 answer

Collectors.toSet() and HashSet

Take the following line of sample code: Set someSet = someColletion.stream().map(p -> p.toString()).collect(Collectors.toSet()); I want a HashSet. Taking a debugger to the code, I am indeed getting a HashSet. I had a look at…
Robert Bain
  • 9,113
  • 8
  • 44
  • 63
92
votes
3 answers

What kind of List does Collectors.toList() return?

I am reading State of the Lambda: Libraries Edition, and am being surprised by one statement: Under the section Streams, there is the following: List blue = shapes.stream() .filter(s -> s.getColor() == BLUE) …
skiwi
  • 66,971
  • 31
  • 131
  • 216
83
votes
5 answers

Java 8 Collectors.toMap SortedMap

I'm using Java 8 lambdas and want to use Collectors toMap to return a SortedMap. The best I can come up with is to call the following Collectors toMap method with a dummy mergeFunction and mapSupplier equal to TreeMap::new. public static
Robert Bain
  • 9,113
  • 8
  • 44
  • 63
77
votes
8 answers

Hashmap with Streams in Java 8 Streams to collect value of Map

Let consider a hashmap Map id1 = new HashMap(); I inserted some values into both hashmap. For Example, List list1 = new ArrayList(); list1.add("r1"); list1.add("r4"); List list2 = new…
Deepak Shajan
  • 911
  • 1
  • 7
  • 19
75
votes
3 answers

Differences of Java 16's Stream.toList() and Stream.collect(Collectors.toList())?

JDK 16 now includes a toList() method directly on Stream instances. In previous Java versions, you always had to use the collect method and provide a Collector instance. The new method is obviously fewer characters to type. Are both methods…
knittl
  • 246,190
  • 53
  • 318
  • 364
70
votes
13 answers

Splitting List into sublists along elements

I have this list (List): ["a", "b", null, "c", null, "d", "e"] And I'd like something like this: [["a", "b"], ["c"], ["d", "e"]] In other words I want to split my list in sublists using the null value as separator, in order to obtain a…
Oneiros
  • 4,328
  • 6
  • 40
  • 69
68
votes
8 answers

Collectors.groupingBy doesn't accept null keys

In Java 8, this works: Stream stream = Stream.of(ArrayList.class); HashMap> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); But this doesn't: Stream stream =…
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
64
votes
2 answers

Java 8 toMap IllegalStateException Duplicate Key

I have a file which contains data in the following format 1 2 3 I want to load this to map as {(1->1), (2->1), (3->1)} This is the Java 8 code, Map map1 = Files.lines(Paths.get(inputFile)) .map(line ->…
pramodh
  • 1,259
  • 1
  • 14
  • 29
61
votes
6 answers

Transform and filter a Java Map with streams

I have a Java Map that I'd like to transform and filter. As a trivial example, suppose I want to convert all values to Integers then remove the odd entries. Map input = new HashMap<>(); input.put("a", "1234"); input.put("b",…
Paul I
  • 860
  • 2
  • 9
  • 16
57
votes
5 answers

What's the purpose of partitioningBy

For example, if I intend to partition some elements, I could do something like: Stream.of("I", "Love", "Stack Overflow") .collect(Collectors.partitioningBy(s -> s.length() > 3)) .forEach((k, v) -> System.out.println(k + " => " +…
user2336315
  • 15,697
  • 10
  • 46
  • 64
50
votes
3 answers

collecting HashMap> java 8

I want to be able to convert a List to a HashMap where the key is the elementName and the values is a list of something random (in this case its the Element Name). So in short I want (A->List(A), B->List(B), C-> List(C)). I tried using toMap() and…
user3869813
  • 749
  • 1
  • 7
  • 12
1
2 3
47 48