Differences between Collectors.toMap() and Collectors.groupingBy() to collect into a Map

TLDR : To collect into a Map that contains a single value by key (Map<MyKey,MyObject>), use Collectors.toMap(). To collect into a Map that contains multiple values by key (Map<MyKey, List<MyObject>>), use Collectors.groupingBy(). Collectors.toMap() By writing : chargePoints.stream().collect(Collectors.toMap(Point::getParentId, c -> c)); The returned object will have the Map<Long,Point> type. Look at the Collectors.toMap() function that you … Read more

Java 8 grouping using custom collector?

When grouping a Stream with Collectors.groupingBy, you can specify a reduction operation on the values with a custom Collector. Here, we need to use Collectors.mapping, which takes a function (what the mapping is) and a collector (how to collect the mapped values). In this case the mapping is Person::getName, i.e. a method reference that returns … Read more

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

NOTE: nosid’s answer shows how to add to an existing collection using forEachOrdered(). This is a useful and effective technique for mutating existing collections. My answer addresses why you shouldn’t use a Collector to mutate an existing collection. The short answer is no, at least, not in general, you shouldn’t use a Collector to modify … Read more

Java 8 Collectors.toMap SortedMap

I don’t think you can get much better than this: .collect(Collectors.toMap(keyMapper, valueMapper, (v1,v2) ->{ throw new RuntimeException(String.format(“Duplicate key for values %s and %s”, v1, v2));}, TreeMap::new)); where the throw lambda is the same as throwingMerger() but I can’t directly call that since it’s package private (you can of course always make your own static method … Read more

Stream.skip behavior with unordered terminal operation

Recall that the goal of stream flags (ORDERED, SORTED, SIZED, DISTINCT) is to enable operations to avoid doing unnecessary work. Examples of optimizations that involve stream flags are: If we know the stream is already sorted, then sorted() is a no-op; If we know the size of the stream, we can pre-allocate a correct-sized array … Read more

Java Streams: Replacing groupingBy and reducing by toMap

This pattern became evident by experience with using both collectors. You’ll find several Q&As on Stackoverflow, where a problem could be solved with either collector, but one of them seems a better fit for the particular task. This is a variation of the difference between Reduction and Mutable Reduction. In the first case, we use … Read more

NullPointerException in Collectors.toMap with null entry values

You can work around this known bug in OpenJDK with this: Map<Integer, Boolean> collect = list.stream() .collect(HashMap::new, (m,v)->m.put(v.getId(), v.getAnswer()), HashMap::putAll); It is not that much pretty, but it works. Result: 1: true 2: true 3: null (this tutorial helped me the most.) EDIT: Unlike Collectors.toMap, this will silently replace values if you have the same … Read more

tech