

A map cannot contain duplicate keys each key can map to at most one value.
If mapping function of this method returns null, the mapping is removed. public interface MapThe traditional way to solve this, is to wrap your put into an if-statement using containsKey. The computeIfPresent(Key, BiFunction) method of HashMap class which allows you to compute value of a mapping for specified key if key is already associated with a value (or is mapped to null). If the map doesn’t have any value associated with the key, it’ll create a NullPointerException. There is a problem with the solution above.

Only modify if key already exists in the map map.compute("Java", (key, value) -> sortAlphabetically(value)) In Java 8, we got a new method - compute - giving us the possibility to define a function describing how we want to change the data for a given key. map.put("Java", sortAlphabetically(map.get("Java"))) Using put, we would first have to get the value from the map, sort the data, then put it back in the map. Let’s start off by sorting the list of articles associated with the Java tag.
#COMPUTE METHOD MAP UPDATE#
This method is used to automatically update a value for given key in HashMap. The compute() method tries to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). We have a Map> that contains tags mapping to their associated articles. The compute(Key, BiFunction) method of HashMap class allows you to update a value in HashMap. To see how they really shine, let’s compare it against the traditional put by solving a few problems.
#COMPUTE METHOD MAP CODE#
Using these methods, we can write more declarative code that is easier to reason about. This has luckily come to an end with Java 8, where we’ve got a lot of new methods that work on a higher level. The compute () method tries to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). This type of imperative code quickly gets bloated, repetitive, and draws focus away from what we actually want done. The compute (Key, BiFunction) method of HashMap class allows you to update a value in HashMap. The syntax of the compute () method is: pute (K key, BiFunction remappingFunction) Here, hashmap is an object of the HashMap class. The typical way we’ve been solving this for maps has been by creating if-statements and other imperative code to do checks against the map before actually updating it. Java HashMap compute () Java HashMap compute () The Java HashMap compute () method computes a new value and associates it with the specified key in the hashmap.
#COMPUTE METHOD MAP HOW TO#
In this Java Tutorial, we have learnt the syntax of Java pute() function, and also learnt how to use this function with the help of examples.If you are familiar with the map interface in Java, you know that put is the traditional way to add new data to a map.īut as you know, when writing your code, an update almost always comes with conditions that have to be checked. Output Before compute : Įxception in thread "main" Īt java.base/(Unknown Source)Īt Example.main(Example.java:12) Conclusion

Also, we shall do the same computation for key "D". Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). Using compute() function, we will compute a value for key "C", such that the new value is square of the existing value. In this example, we will initialize a HashMap with mappings from String to Integer. The function returns V Example 1 – compute() The mapping function to compute a value for the specified key. Additionally, if the value returned by the BiFunction is null, the method will remove the mapping. The key with which the specified value is to be associated. The syntax of compute() function is compute(K key, BiFunction remappingFunction) pute() computes a mapping for the specified key and its current mapped value (or null if there is no current mapping).
