Search code examples
javalistcollectionshashmapjava.util.concurrent

Does ConcurrentHashMap implementation use cloning in remove operation?


I was reading an IBM DeveloperWorks article to understand how ConcurrentHashMap is implemented. The section "removal operation" suggests that the removal operation is a two step process:

  1. Find the Entry element and make it null
  2. The portion of the chain from the head to the removed element is cloned and joined to the remainder of the chain following the removed element.

I have a question here though, because I examined the the corresponding code at Docjar and I don't see any cloning in remove(Object key, int hash, Object value).

I am not sure if I am missing something, or the implementation is different from the article?


Solution

  • The line

    head = new Entry(p.hash, p.key, p.value, head);
    

    does the cloning, it is not an "Object.clone()" cloning, but a "copy constructor" - this is still cloning in a more general sense.

    Note that this line is in the 10-years old article, and cannot be found in the current implementation, where the implementation of remove(Object key, Object value) is completely different.