Important points about compute() method of Java 8 Here is the screenshot of this program with output: You can further see The Ultimate Java 8 Tutorial - From beginner to professional to learn more about LongAdder and several other useful new classes added in JDK 8 API. That's why when we printed map2, we see that there are two apples and 1 mango is present in our concurrent map. This is a much better way to increase a counter in a concurrent Java application, but LongAdder is only available from Java 8. it was absent initially, after that, the count is incremented in the LongAdder object. The good thing about this method is that the LongAdder object is only created when a key is first time added i.e. After that, we have added 2 apples and 1 mango to this map using computeIfAbsent() method and it has created a LongAdder object for each key and stored that as a value. This is suited for scenarios like you are doing a sale and you need to keep track of how many copies of a particular book or course are sold.Īnother useful scenario is reading through a text file and printing the count of each word that appears in the file.Īnyway, the key point here is that the map is initially empty which is shown in the third print statement. In the second example, we have an empty ConcurrentHashMap and our job is to add keys and their counts in real-time.
This is just incrementing the value for this key, hence the count of "apple" moved from 3 to 4, which is shown in our second print statement. Map.compute( "apple", ( key, value) -> value = null ? 1 : value + 1)
#Compute method map update
Now, we got one more apple and we need to update the count on our bucket, for that, we are calling the compute() method as shown below:
In the first line, we have simply created a Concurrent HashMap with two keys apple and mango, and their values are their count like how many apples we have and how many mangoes we have. Now, let's try to understand what's happening here. LongAdder /* * Java Program to use compute() method of Java 8 * to atomically update values in ConcurrentHashMap */ public class Hello You can further see The Complete Java MasterClass course on Udemy by Tim Buchalaka and his team to learn more about new methods added to the existing API in Java 8. If a value exists then it is returned from ConcurrentHashMap, similar to the putIfAbsent() method. Here the constructor of LongAdder class will only be called when a new counter is actually needed. Map.computeIfAbsent( key, k -> new LongAdder()).increment() There are also variants of compute() method like computeIfPresent() and computeIfAbsent(), which computes the new value only if an existing value is present or absent.įor example, you can update a map of LongAdder using computeIfAbsent as shown below: If the function itself throws an ( unchecked) exception, the exception is rethrown, and the current mapping is left unchanged If the function returns null, the mapping is removed (or remains absent if initially absent). Map.compute (key, (k, v) -> (v = null) ? msg : v.concat(msg)) For example, to either create or append a String msg to a value mapping: You can use the compute() method to update an existing value inside ConcurrenHashMap. In simple words, you can use this method to atomically update a value for a given key in the ConcurrentHashMap. Some attempted update operations on this map by other threads may be blocked while the computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this Map. The entire function is performed atomically.
As per Java documentation, The compute() function tries to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). Out of many useful methods, one method which stands out to me is the compute() method, which allows you to update a value in ConcurrentHashMap atomically. Thanks to default methods, the much-needed evolution of existing interfaces becomes possible. The JDK 8 has added several useful methods in existing interfaces e.g.