Let's take a look at a more complex parsing example. One more ComputeIfAbsent() exampleIf you are familiar with the concept of caching, the computeIfAbsent() method probably reminds you of something. (myMap.get("here is my key")) Īs you can see, the value remains unchanged. puteIfAbsent("here is my key", key -> key + ", " + "and this is a new value")
If (newValue != null) map.put(key, newValue) ĬomputeIfAbsent() code exampleSo, if the value is not in the Map, then the method will perform the changes.
Else if the key doesn’t represent in Map (or it’s null) the method calculates the value using mappingFunction to the key.If the key is represented in Map (and it’s not null), then the method does nothing.The method first checks if the passed key is represented in our Map.Here is the logical algorithm of the method: How the computeIfAbsent() method worksAs we already know, the Map.computeIfAbsent() method is passed two parameters, the key and the function for calculating the value for this key mappingFunction. In this method the mapping function is only called if the mapping is not presented. The second parameter is the mappingFunction. The Map (and HashMap) computeIfAbsent() method takes two parameters. We will consider it in this article.ĭefault V computeIfAbsent(K key, Function mappingFunction) If you need to do something with a value that isn’t in Map, you can use the ComputeIfAbsent() method. So, if you need to perform some action with the value in the Map, if it exists in it, there is a ComputeIfPresent() method in Java Map. However, Java 8 added several useful methods to the Map interface itself to reduce the amount of "bad" code.
This is a very useful toolkit, but it has a drawback, it does not include Maps. if (map.Java 8, as many of us know, introduced the Stream API. The traditional way to solve this, is to wrap your put into an if-statement using containsKey. 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.
We have a Map> that contains tags mapping to their associated articles. To see how they really shine, let’s compare it against the traditional put by solving a few problems. 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. This type of imperative code quickly gets bloated, repetitive, and draws focus away from what we actually want done. 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.
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.