Incrementing a Map’s value in Java
This post will discuss several methods to increment a key’s value of a map in Java. If no mapping is present for the specified key in the map, map the key to a value equal to 1.
1. Checking for null
A simple solution is to check if the map contains the mapping for the specified key or not. If the mapping is not present, simply map the key with a value of 1; if the mapping is present, 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 24 25 26 27 28 29 30 31 32 33 |
import java.util.HashMap; import java.util.Map; // Program to increment a key's value of a map in Java class Main { public static<K> void incrementValue(Map<K, Integer> map, K key) { // get the value of the specified key Integer count = map.get(key); // if the map contains no mapping for the key, // map the key with a value of 1 if (count == null) { map.put(key, 1); } // else increment the found value by 1 else { map.put(key, count + 1); } } public static void main(String[] args) { Map<String, Integer> map = new HashMap(); map.put("A", 100); incrementValue(map, "A"); incrementValue(map, "B"); System.out.println(map); } } |
Output:
{A=101, B=1}
The above solution makes two calls to the put() method. We can avoid that by tweaking the solution a little, 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 24 25 26 27 28 29 30 31 32 |
import java.util.HashMap; import java.util.Map; // Program to increment a key's value of a map in Java class Main { public static<K> void incrementValue(Map<K, Integer> map, K key) { // get the value of the specified key Integer count = map.get(key); // if the map contains no mapping for the key, // then initialize its value as 0 if (count == null) { count = 0; } // increment the key's value by 1 map.put(key, count + 1); } public static void main(String[] args) { Map<String, Integer> map = new HashMap(); map.put("A", 100); incrementValue(map, "A"); incrementValue(map, "B"); System.out.println(map); } } |
Output:
{A=101, B=1}
2. Using containsKey() method
This approach is also similar to the previous method but uses the containsKey() method to check if the map contains a mapping for a key or not.
|
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 key's value of a map in Java class Main { public static<K> void incrementValue(Map<K, Integer> map, K key) { // containsKey() checks if this map contains a mapping for a key Integer count = map.containsKey(key) ? map.get(key) : 0; map.put(key, count + 1); } public static void main(String[] args) { Map<String, Integer> map = new HashMap(); map.put("A", 100); incrementValue(map, "A"); incrementValue(map, "B"); System.out.println(map); } } |
Output:
{A=101, B=1}
3. Using AtomicInteger or MutableInt class
We can also use AtomicInteger class and call getAndIncrement() or incrementAndGet() method to increment the value, as shown below. Another alternative could be to use MutableInt class which provides increment() method.
|
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; import java.util.concurrent.atomic.AtomicInteger; // Program to increment a key's value of a map in Java class Main { public static<K> void incrementValue(Map<K, Integer> map, K key) { AtomicInteger atomic = new AtomicInteger(map.containsKey(key) ? map.get(key) : 0); map.put(key, atomic.incrementAndGet()); } public static void main(String[] args) { Map<String, Integer> map = new HashMap(); map.put("A", 100); incrementValue(map, "A"); incrementValue(map, "B"); System.out.println(map); } } |
Output:
{A=101, B=1}
That’s all about incrementing Map’s value in Java.
Must Read:
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 :)