Add values to a Map in Java
This post will discuss how to add values to a Map in Java.
1. Add values to a Map
The standard solution to add values to a map is using the put() method, which associates the specified value with the specified key in the map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, String> colors = new HashMap<>(); colors.put("Black", "#000000"); colors.put("White", "#FFFFFF"); colors.put("Red", "#FF0000"); System.out.println(colors); } } |
Output:
{Red=#FF0000, White=#FFFFFF, Black=#000000}
Note that if the map already contains a mapping corresponding to the specified key, the old value will be replaced by the specified value. To avoid this behavior, use the putIfAbsent() method instead, which associates the specified key with the specified value if and only of the key is not already present in the map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, String> colors = new HashMap<>(); colors.put("Black", "#000000"); colors.put("White", "#FFFFFF"); colors.put("Red", "#FF0000"); colors.putIfAbsent("White", "#FFF"); System.out.println(colors); } } |
Output:
{Red=#FF0000, White=#FFFFFF, Black=#000000}
2. Add values to a MultiMap
There is no direct method to add a key-value pair to a map whose values are a mutable collection. However, you can do something like the below:
|
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 |
import java.util.*; public class Main { public static void insert(Map<String, List<String>> map, String key, List<String> value) { if (map.containsKey(key)){ map.get(key).addAll(value); } else { map.put(key, value); } } public static void main(String[] args) { Map<String, List<String>> colors = new HashMap<>(); colors.put("Black", new ArrayList<>(Arrays.asList("#000000", "#000"))); colors.put("White", new ArrayList<>(Arrays.asList("#FFFFFF", "#ffffff"))); colors.put("Red", new ArrayList<>(Arrays.asList("#FF0000", "#ff0000"))); insert(colors, "White", Arrays.asList("#FFF", "#fff")); System.out.println(colors); } } |
Output:
{Red=[#FF0000, #ff0000], White=[#FFFFFF, #ffffff, #FFF, #fff], Black=[#000000, #000]}
Note that UnsupportedOperationException is thrown if the underlying list is immutable. We can shorten the above code using the computeIfAbsent() method by Stream API.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; public class Main { public static void main(String[] args) { Map<String, List<String>> colors = new HashMap<>(); colors.put("Black", new ArrayList<>(Arrays.asList("#000000", "#000"))); colors.put("White", new ArrayList<>(Arrays.asList("#FFFFFF", "#ffffff"))); colors.put("Red", new ArrayList<>(Arrays.asList("#FF0000", "#ff0000"))); colors.computeIfAbsent("White", i -> new ArrayList<>()) .addAll(Arrays.asList("#FFF", "#fff")); System.out.println(colors); } } |
Output:
{Red=[#FF0000, #ff0000], White=[#FFFFFF, #ffffff, #FFF, #fff], Black=[#000000, #000]}
That’s all about adding values to a 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 :)