Increment a Map’s value in Java 8 and above
This post will explore ways to increment a map value in Java 8 and above. If the map contains the mapping for the specified key, the solution should increment its value by 1; otherwise, it should associate 1 with the specified key.
In the previous post, we have seen how to increment a key’s value of a map in Java 7 or less. This post covers a bunch of useful methods introduced in the Map interface with Java 8, such as putIfAbsent(), merge(), getOrDefault() and computeIfPresent(), which can be used to increment a map value.
1. Using putIfAbsent() method
The putIfAbsent() method associates the specified key with the given value if it is not already associated with a value. We can use this method to check if a mapping exists for the specified key or not. If not, we create the mapping of the specified key with the value 0. Finally, we increment the key’s value by 1.
|
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.HashMap; import java.util.Map; // Program to increment a map value in Java 8 and above class Main { public static<K> void increment(Map<K, Integer> map, K key) { map.putIfAbsent(key, 0); map.put(key, map.get(key) + 1); } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); increment(hashMap, "A"); increment(hashMap, "B"); System.out.println(hashMap); } } |
Output:
{A=2, B=1}
2. Using merge() method
We can also use the merge() method, where the remapping method increments the existing value by the specified value of 1.
|
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.HashMap; import java.util.Map; // Program to increment a map value in Java 8 and above class Main { public static<K> void increment(Map<K, Integer> map, K key) { map.merge(key, 1, Integer::sum); // or use lambda expressions // map.merge(key, 1, (a, b) -> a + b); } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); increment(hashMap, "A"); increment(hashMap, "B"); System.out.println(hashMap); } } |
Output:
{A=2, B=1}
3. Using getOrDefault() method
The getOrDefault() method returns the value of the specified key or returns the specified default value if no mapping exists for the key in the map. We can make use of this method, as demonstrated below:
|
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.HashMap; import java.util.Map; // Program to increment a map value in Java 8 and above class Main { public static<K> void increment(Map<K, Integer> map, K key) { Integer count = map.getOrDefault(key, 0); map.put(key, count + 1); } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); increment(hashMap, "A"); increment(hashMap, "B"); System.out.println(hashMap); } } |
Output:
{A=2, B=1}
4. Using computeIfPresent() method
The computeIfPresent() returns null if no value is associated with the specified key; otherwise, it computes a new mapping given the key and its existing 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 |
import java.util.HashMap; import java.util.Map; // Program to increment a map value in Java 8 and above class Main { public static<K> void increment(Map<K, Integer> map, K key) { if (map.computeIfPresent(key, (k, v) -> v + 1) == null) { map.put(key, 1); } } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); increment(hashMap, "A"); increment(hashMap, "B"); System.out.println(hashMap); } } |
Output:
{A=2, B=1}
That’s all about incrementing a Map Value 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 :)