This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Why is there no ConcurrentHashSet for ConcurrentHashMap?

HashSet is based on HashMap.

If we look at the HashSet implementation, everything is managed under HashMap<E,Object>.

Used as a key HashMap.

And we know that this HashMap is not thread-safe. That’s why we use ConcurrentHashMapJava.

Based on this, I’m confused as to why we don’t have a ConcurrentHashSet ConcurrentHashMap that we should be based on.

Is there anything else I miss? I need Set to work in a multithreaded environment.

In addition, if I want to create your own applications, whether ConcurrentHashSet can only replace HashMaptoConcurrentHashMap implemented and the rest will remain the same?

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

There is no built-in type, ConcurrentHashSet because you can always derive a collection from a Map. Because there are many types of Map, you can use a method to generate a collection from a given Map (or Map class).

Prior to Java 8, you could use the generation of concurrent hash sets supported by concurrent hash graphs. Collections.newSetFromMap(map)

In Java 8 (by @ matt pointed out), you can get a hash of concurrent collection view through ConcurrentHashMap. NewKeySet (). This is a little simpler than the old version of newSetFromMap, which required you to pass in an empty Map object. But this is specific to ConcurrentHashMap.

In any case, Java designers can create a new SET interface every time they create a new Map interface, but this pattern will not work when a third party creates its own Map. It is better to have a static way of deriving new sets. This method works even if you create your own Map implementation.

Answer:

Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); tion<User> authorizedUsers = filter(allUsers, isAuthorized);Copy the code

The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 6…

ConcurrentHashMap can be converted to set

Hashmap contains a lot of topics, such as jdk1.7’s head insertion loop problem, 1.8’s tail insertion problem, the initial capacity of hashmap, expansion, hash algorithm, thread safety (concurrenthashmap-cas +sync), and the calculation of size.

JDK1.8 size is obtained by CAS calculation of baseCount and counterCell, and finally by baseCount and counterCell traversal. The main idea is to improve throughput through arrays.

It is recommended to read the source code several times to deepen the impression.


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️