Convert Set to Map in Java
This post will discuss how to convert a set to a map in Java.
Consider the following Color class, which contains two private fields – colorName and colorCode. They represent the color name and its corresponding HTML color code, respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Color { private String colorName; private String colorCode; public Color(String colorName, String colorCode) { this.colorName = colorName; this.colorCode = colorCode; } @Override public String toString() { return colorName + "=" + colorCode; } // getters and setters } |
Let’s construct a map from set of Color objects using colorName as key and colorCode as value:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import java.util.HashSet; import java.util.HashMap; import java.util.Set; import java.util.Map; class Color { private String name; private String code; public Color(String name, String code) { this.name = name; this.code = code; } @Override public String toString() { return name + "=" + code; } // getters and setters public String getCode() { return code; } public String getName() { return name; } } class Main { // program to convert set to map in Java public static void main(String[] args) { // the input set of `Color` objects Set<Color> colors = new HashSet<>(); colors.add(new Color("WHITE", "#FFFFFF")); colors.add(new Color("GRAY", "#808080")); colors.add(new Color("BLACK", "#000000")); Map<String, String> colorMap = new HashMap<>(); // construct key-value pairs from `name` and `code` fields of `Color` class for (Color ob: colors) { colorMap.put(ob.getName(), ob.getCode()); } System.out.println("Set : " + colors); System.out.println("Map : " + colorMap); } } |
Output:
Set : [WHITE=#FFFFFF, GRAY=#808080, BLACK=#000000]
Map : {WHITE=#FFFFFF, GRAY=#808080, BLACK=#000000}
Using Java 8 Stream
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; class Color { private String name; private String code; public Color(String name, String code) { this.name = name; this.code = code; } @Override public String toString() { return name + "=" + code; } // getters and setters public String getCode() { return code; } public String getName() { return name; } } class Main { // program to convert set to map in Java public static void main(String[] args) { // the input set of `Color` objects Set<Color> colors = new HashSet<>(); colors.add(new Color("WHITE", "#FFFFFF")); colors.add(new Color("GRAY", "#808080")); colors.add(new Color("BLACK", "#000000")); // construct key-value pairs from `name` and `code` fields of `Color` class Map<String, String> colorMap = colors.stream().collect( Collectors.toMap(x -> x.getName(), x -> x.getCode())); System.out.println("Set : " + colors); System.out.println("Map : " + colorMap); } } |
Output:
Set : [WHITE=#FFFFFF, GRAY=#808080, BLACK=#000000]
Map : {WHITE=#FFFFFF, GRAY=#808080, BLACK=#000000}
We can also use method references instead of lambda expressions.
|
1 |
colors.stream().collect(Collectors.toMap(Color::getColorName, Color::getColorCode)); |
Collectors class provides several overloaded versions of toMap() method:
1. If there are duplicate keys present on the map, we can provide a merge method used to resolve collisions between values associated with the same key.
|
1 2 3 |
colors.stream().collect(Collectors.toMap(Color::getColorName, Color::getColorCode, (oldValue, newValue) -> newValue)); |
2. The default implementation of
toMap() doesn’t specify the Type of the map object returned. We can supply the Type of map in another overloaded version of the toMap() method, as shown below:
|
1 2 3 4 |
colors.stream().collect(Collectors.toMap(Color::getColorName, Color::getColorCode, (oldValue, newValue) -> newValue, TreeMap::new)); |
Here’s is one more example where we have a set of specific ASCII characters, and we’ll construct a map out of it that represent the ASCII character table.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; class Main { // Program to convert set to map in Java public static void main(String[] args) { // set of ASCII characters Set<String> chars = new HashSet<>(Arrays.asList("A", "B", "C", "D")); // Map to represent the ASCII character table Map<String, Integer> asciiMap = null; asciiMap = chars.stream().collect(Collectors.toMap(Function.identity(), String::hashCode)); System.out.println(asciiMap); } } |
Output:
{A=65, B=66, C=67, D=68}
That’s all about converting Set to Map in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)