ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()

拟墨画扇 提交于 2019-12-20 11:52:57

问题


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?


回答1:


ConcurrentHashMap.newKeySet() should be somewhat more efficient as removes a single level of indirection. Collections.newSetFromMap(map) is mostly based on redirecting the operations to the map.keySet(), but ConcurrentHashMap.newKeySet() is very close to map.keySet() itself (just with additions support).

As for functionality, I see no difference.




回答2:


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…



来源:https://stackoverflow.com/questions/32054517/concurrenthashmap-newkeyset-vs-collections-newsetfrommap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!