Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 <T, K, U, M extends Map<K, U>>         Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,                 Function<? super T, ? extends U> valueMapper,                 BinaryOperator<U> mergeFunction,                 Supplier<M> mapSupplier) {     BiConsumer<M, T> accumulator = (map, element) -> map.merge(keyMapper.apply(element),             valueMapper.apply(element), mergeFunction);     return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID); } 

I don't want to pass in a merge function though, as I just want throwingMerger(), in the same way as the basic toMapimplementation as follows:

public static <T, K, U>         Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper,                 Function<? super T, ? extends U> valueMapper) {     return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new); } 

What would be the best practise method of using Collectors to return a SortedMap?

like image 516
Robert Bain Avatar asked Jun 23 '15 14:06

Robert Bain


People also ask

How does collectors toMap work?

Collectors toMap() method in Java with Examples. The toMap() method is a static method of Collectors class which returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.

How do you collect from TreeMap?

Collect the data directly to a TreeMap by using the overloaded toMap(keyMapper, valueMapper, mergeFunction, mapSupplier) method that allows you to specify which Map to create (4th parameter).

What is the use of collectors in Java 8?

collect() is one of the Java 8's Stream API's terminal methods. It allows us to perform mutable fold operations (repackaging elements to some data structures and applying some additional logic, concatenating them, etc.) on data elements held in a Stream instance.

How do I collect a stream map?

Method 1: Using Collectors.toMap() Function The Collectors. toMap() method takes two parameters as the input: KeyMapper: This function is used for extracting keys of the Map from stream value. ValueMapper: This function used for extracting the values of the map for the given key.


1 Answers

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 for that like throwingMerger() is. )

like image 104
dkatzel Avatar answered Oct 12 '22 01:10

dkatzel