Collectors.toMap() method is used with Java Streams to convert stream elements into a Map. It creates map keys and values by applying the given functions to each element of the stream.
- Keys must be unique.
- If duplicate keys occur and no merge function is provided, an IllegalStateException is thrown.
1. toMap(keyMapper, valueMapper)
Convert a stream to a Map when all keys are unique.
Syntax
Collectors.toMap(Function keyMapper, Function valueMapper)
Parameters:
- keyMapper: maps stream elements to keys
- valueMapper: maps stream elements to values
Return Type: Collector<T, ?, Map<K, V>>
Example: This example demonstrates how to use Collectors.toMap() to convert a stream of key–value pairs into a Map when all keys are unique.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args) {
Stream<String[]> stream = Stream.of(
new String[][]{
{"GFG", "GeeksForGeeks"},
{"g", "geeks"}
}
);
Map<String, String> map = stream.collect(Collectors.toMap(p -> p[0], p -> p[1]));
System.out.println(map);
}
}
Output
{g=geeks, GFG=GeeksForGeeks}
Explanation:
- The stream contains key–value pairs stored as string arrays.
- Collectors.toMap(p -> p[0], p -> p[1]) maps the first element as the key and the second element as the value.
2. toMap(keyMapper, valueMapper, mergeFunction)
Handle duplicate keys in the stream.
Syntax
Collectors.toMap(Function keyMapper, Function valueMapper, BinaryOperator<U> mergeFunction)
Parameters:
- keyMapper: produces keys
- valueMapper: produces values
- mergeFunction: resolves duplicate key conflicts
Return Type: Collector<T, ?, Map<K, V>>
Example: This code demonstrates using Collectors.toMap() to handle duplicate keys.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args) {
Stream<String[]> stream = Stream.of(
new String[][]{
{"GFG", "GeeksForGeeks"},
{"GFG", "Geeks"},
{"g", "geeks"}
}
);
LinkedHashMap<String, String> map =
stream.collect(Collectors.toMap(
p -> p[0],
p -> p[1],
(v1, v2) -> v1 + ", " + v2,
LinkedHashMap::new
));
System.out.println(map);
}
}
Output
{GFG=GeeksForGeeks, Geeks, g=geeks}
Explanation:
- The stream contains key–value pairs with the same key ("GFG"), which would normally cause an error.
- p -> p[0] extracts the key and p -> p[1] extracts the value.
- The merge function (v1, v2) -> v1 + ", " + v2 combines values of duplicate keys.
- The result is a Map where duplicate values are merged instead of throwing an exception.
3. toMap(keyMapper, valueMapper, mergeFunction, mapSupplier)
Handle duplicates and use a custom Map implementation (e.g., LinkedHashMap).
Syntax
Collectors.toMap(Function keyMapper, Function valueMapper,BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
Parameters:
- keyMapper: maps keys
- valueMapper: maps values
- mergeFunction: handles duplicate keys
- mapSupplier: supplies the Map type
Return Type: Collector<T, ?, M extends Map<K, V>>
Example: This example demonstrates using Collectors.toMap() to handle duplicate keys and maintain insertion order with a LinkedHashMap.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args) {
Stream<String[]> stream = Stream.of(
new String[][]{
{"GFG", "GeeksForGeeks"},
{"GFG", "Geeks"},
{"g", "geeks"}
}
);
LinkedHashMap<String, String> map =
stream.collect(Collectors.toMap(
p -> p[0],
p -> p[1],
(v1, v2) -> v1 + ", " + v2,
LinkedHashMap::new
));
System.out.println(map);
}
}
Output
{GFG=GeeksForGeeks, Geeks, g=geeks}
Explanation:
- The stream contains key–value pairs with duplicate keys ("GFG").
- The merge function (v1, v2) -> v1 + ", " + v2 combines values for duplicate keys.
- LinkedHashMap::new ensures the order of insertion is preserved in the final map.