ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()

turbanoff picture turbanoff · Aug 17, 2015 · Viewed 9.2k times · Source

Java 8 introduced new way to obtain a concurrent Set implementation

// Pre-Java-8 way to create a concurrent set
Set<String> oldStyle = Collections.newSetFromMap(new ConcurrentHashMap<>());
// New method in Java 8
Set<String> newStyle = ConcurrentHashMap.newKeySet();

Is there any reason to prefer new method?

Any advantages/disadvantages?

Answer

Holger picture Holger · Aug 17, 2015

ConcurrentHashMap.newKeySet() is just a part of a feature which is much broader than Collections.newSetFromMap(new ConcurrentHashMap<>()).

The difference becomes clear if you look at this example:

Set<String> set=new ConcurrentHashMap<String,String>().keySet("hello");

Instead of mapping to Boolean.TRUE you are now adding the value "hello" when adding a new value to the Set.

That’s why the returned Sets have the type ConcurrentHashMap.KeySetView. This type has additional methods for asking for the backing map as well as which value will be used when adding new keys.


So while ConcurrentHashMap.newKeySet() looks like doing the same as Collections.newSetFromMap(new ConcurrentHashMap<>()), there is the semantic difference that the latter says you shouldn’t use the map afterwards while the former is part of a feature which is designed to interact with the map.

See Collections.newSetFromMap:

The specified map must be empty at the time this method is invoked, and should not be accessed directly after this method returns.

In fact, it is not even specified that Collections.newSetFromMap will use Boolean.TRUE for added values—you should never deal with that anyway…


It might also be useful when you wan to pass the Set to code which explicitly requests a ConcurrentHashMap.KeySetView.


If you are using the result using the compile-time type Set only, there is still the possibility that code which receives that Set will use instanceof/type casts to find out that the result of ConcurrentHashMap.newKeySet() is backed by a ConcurrentHashMap while the result of Collections.newSetFromMap won’t tell you. On the other hand, that also allows code to do unintended things with backing map that way…