Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConcurrentHashMap JDK 8 when to use computeIfPresent

Tags:

The new version of Concurrent Hash Map of jdk 8 has two new Methods.

computeIfAbsent

computeIfPresent

putIfAbsent - Old method

I understand the use cases of putIfAbsent and computeIfAbsent. But i am not sure of the scenarios when i will use computeIfPresent. Also why do i need putIfAbsent if i have computeIfPresent now. putIfAbsent do create atleast one extra instance of the value.

Is the reason is only to have backward compatability?

like image 825
veritas Avatar asked Jul 21 '14 18:07

veritas


People also ask

When should we use ConcurrentHashMap?

You should use ConcurrentHashMap when you need very high concurrency in your project. It is thread safe without synchronizing the whole map . Reads can happen very fast while write is done with a lock. There is no locking at the object level.

What is computeIfPresent?

The Java HashMap computeIfPresent() method computes a new value and associates it with the specified key if the key is already present in the hashmap. The syntax of the computeIfPresent() method is: hashmap.computeIfPresent(K key, BiFunction remappingFunction) Here, hashmap is an object of the HashMap class.

What is the use of ConcurrentHashMap () in multithreading?

ConcurrentHashMap class is thread-safe i.e. multiple threads can operate on a single object without any complications. At a time any number of threads are applicable for a read operation without locking the ConcurrentHashMap object which is not there in HashMap.

Does ConcurrentHashMap uses object level lock?

ConcurrentHashMap was introduced in JDK 5. There is no locking at the object level,The locking is at a much finer granularity.


1 Answers

As mentioned in the other answer: Methods will always be kept for backward compatibility, even if there are new, more "powerful" methods introduced.

Concerning the use case for computeIfPresent: It may be hard to find an example that is small enough to not look contrived and still be convincing. In general, the intention of this method is to update an existing value in any form.

One example could be a (constrained) word count: For a given set of words, one stores an initial count of 0 in the map. Then, a sequence of words is processed: Whenever one finds a word from the initial set, its count is increased by 1:

import java.util.LinkedHashMap; import java.util.Map;  public class ComputeIfPresentExample  {     public static void main(String[] args)      {         Map<String, Integer> wordCounts = new LinkedHashMap<String, Integer>();          String s =              "Lorem ipsum dolor sit amet consetetur iam nonumy sadipscing " +              "elitr, sed diam nonumy eirmod tempor invidunt ut erat sed " +              "labore et dolore magna dolor sit amet aliquyam erat sed diam";          wordCounts.put("sed", 0);         wordCounts.put("erat", 0);          for (String t : s.split(" "))         {             wordCounts.computeIfPresent(t, (k,v) -> v+1);         }         System.out.println(wordCounts);     } } 

(Of course, things like this could be solved differently, but this is a rather frequent task in one or the other form, and the new method allows a rather concise and elegant solution)

like image 150
Marco13 Avatar answered Sep 23 '22 05:09

Marco13