Btw, changing a price is little bit totally different than updating a price. In case of change, you understand the brand new worth and also you needn’t retrieve the outdated worth related to the important thing, however in case of replace you first must retrieve the prevailing worth for a given key, modify it e.g. increment or decrement it by 1 after which put it again once more into the map.
hashmap.put(key, hashmap.get(key) + 1);
int worth = hashmap.get(key);
if (worth == null) {
hashmap.put(key, 0);
} else {
hashmap.put(key, worth + 1);
}
if (hashmap.containsKey(key)) {
hashmap.put(key, hashmap.get(key) + 1);
} else {
hashmap.put(key, 1);
}
This code is not going to throw NullPointerException as we’re checking for the existence of key earlier than we retrieve worth from the Map.
map.put(key, map.getOrDefault(key, 0) + 1);
This code makes use of the getOrDefault() technique of HashMap that retrieves the worth for a key, but when the important thing cannot be retrieved it returns the desired default worth (on this case a ‘0’).
Java Program to replace a key worth in HashMap
Right here is our full Java program to replace a price for given key in HashMap in each Java 8 and earlier than. The Java 8 approach is less difficult and clear however if you’re not working on Java
import java.util.HashMap;
import java.util.Map;
public class Demo {
public static void essential(String[] args) throws Exception {
Map < String, Integer > wordCount = new HashMap < String, Integer > ();
wordCount.put("Java", 0);
wordCount.put("Java", wordCount.get("Java") + 1);
System.out.println("map earlier than replace: " + wordCount);
if (wordCount.containsKey("Java")) {
wordCount.put("Java", wordCount.get("Java") + 1);
} else {
wordCount.put("Java", 0);
}
System.out.println("map after replace: " + wordCount);
wordCount.put("Java", wordCount.getOrDefault("Java", 0) + 1);
System.out.println("map after replace in Java 8: " + wordCount);
}
}
Output
map earlier than replace: {Java=1}
map after replace: {Java=2}
map after replace in Java 8: {Java=3}
That is all about find out how to replace values in HashMap in Java. As I mentioned, In an effort to replace, you should first verify if key exists after which get the worth to keep away from NullPointerExceptoin. The identical put() technique is used to each insert and replace key worth pair in HashMap.
From Java 8 onwards, utilizing getOrDefault(key) as a substitute of get(key) is healthier alternative as you needn’t verify if a key exists in map or not. This technique return a default worth versus null which makes it safer to make use of.
If you wish to be taught extra about such helpful adjustments in Java 8 See Java SE 8 for Actually Impatient, certainly one of my favourite e book for Java 8.
Different Java HashMap tutorials you could prefer to discover:
- How does get() technique of HashMap work in Java? (reply)
- Distinction between ArrayList and HashMap? (distinction)
- 3 methods to loop over a Map in Java? (instance)
- HashSet vs HashMap in Java? (reply)
- How HashSet internally works in Java? (reply)
- HashMap vs LinkedHashMap in Java? (reply)
- ArrayList vs HashMap in Java? (reply)
- Distinction between ConcurrentHashMap and HashMap in Java? (reply)
- The best way to type the HashMap on keys and values in Java? (answer)
- How ConcurrentHashMap internally works in Java? (reply)
- HashMap vs ConcurrentHashMap in Java? (reply)
- One of the best ways to iterate over HashMap in Java? (reply)
- Distinction between HashMap vs Hashtable in Java? (reply)
- The best way to convert Map to Checklist in Java? (answer)
Thanks for studying this text up to now. For those who like this instance then please share with your pals and colleagues.