Close

Java 8 Streams - Collectors.toMap Examples

Java 8 Streams Java Java API 


java.lang.Objectjava.lang.Objectjava.util.stream.Collectorsjava.util.stream.CollectorsLogicBig

The static overloaded methods, Collectors.toMap() return a Collector which produces a new instance of Map, populated with keys per provided keyMapper function and values per provided valueMap function.

<T,K,U> Collector<T,?,Map<K,U>> toMap(

Function<? super T,? extends K> keyMapper,

Function<? super T,? extends U> valueMapper)

<T,K,U> Collector<T,?,Map<K,U>> toMap(

Function<? super T,? extends K> keyMapper,

Function<? super T,? extends U> valueMapper,

BinaryOperator<U> mergeFunction)

<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)

Parameters

keyMapper: A mapping function to produce the map keys for each input stream element.

valueMapper: A mapping function to produce the map values for each input stream element.

mergeFunction: A binary operator which is to resolve collisions between values associated with the same key. The inputs to this function are the values which belong to the same key.

mapSupplier: A function which provides a new instance of the desired implementation of the Map.


Examples


package com.logicbig.example.collectors;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ToMapExample {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "orange");
Map<Character, String> m = s.collect(
Collectors.toMap(s1 -> s1.charAt(0),
s1 -> s1));
System.out.println(m);
}
}

Output

{a=apple, b=banana, o=orange}
Original Post




The method Collectors.toMap(keyMapper, valueMapper) will throw IllegalStateException if there are duplicate keys as provided by keyMapper function.

package com.logicbig.example.collectors;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ToMapExample1 {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "apricot", "orange");
Map<Character, String> m = s.collect(
Collectors.toMap(s1 -> s1.charAt(0),
s1 -> s1));
System.out.println(m);
}
}

Output

Caused by: java.lang.IllegalStateException: Duplicate key apple
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1253)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.logicbig.example.collectors.ToMapExample1.main(ToMapExample1.java:17)
... 6 more
Original Post




package com.logicbig.example.collectors;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ToMapExample2 {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "apricot", "orange",
"apple");
Map<Character, String> m = s.collect(
Collectors.toMap(s1 -> s1.charAt(0),
s1 -> s1,
(s1, s2) -> s1 + "|" + s2));
System.out.println(m);
}
}

Output

{a=apple|apricot|apple, b=banana, o=orange}
Original Post




package com.logicbig.example.collectors;

import java.util.LinkedHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ToMapExample3 {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "apricot", "orange",
"apple");
LinkedHashMap<Character, String> m = s.collect(
Collectors.toMap(s1 -> s1.charAt(0),
s1 -> s1,
(s1, s2) -> s1 + "|" + s2,
LinkedHashMap::new));
System.out.println(m);
}
}

Output

{a=apple|apricot|apple, b=banana, o=orange}
Original Post




See Also