Is there any way to convert java8 Map<K,V>
to ConcurrentMap<K,V>
other than iterating manually over all entities?
Asked
Active
Viewed 1,971 times
-2

Som Bhattacharyya
- 3,972
- 35
- 54

Elad Benda
- 35,076
- 87
- 265
- 471
-
http://stackoverflow.com/questions/510632/whats-the-difference-between-concurrenthashmap-and-collections-synchronizedmap – Hannes Mar 28 '16 at 10:02
-
accidentally asked the question – Elad Benda Mar 28 '16 at 10:08
3 Answers
3
There exist the constructor in https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html
So you can
ConcurrentHashMap(Map m) Creates a new map with the same mappings as the given map.

anquegi
- 11,125
- 4
- 51
- 67
0
You could use the ConcurrentHashMap(Map<? extends K,? extends V> m)
constructor for example to pass in a Map
and construct a ConcurrentHashMap
.

Som Bhattacharyya
- 3,972
- 35
- 54
0
If the only thing you need is synchronized view of this map you can use
java.util.Collections#synchronizedMap
Usually, it isn't what you want, because the performance of "synchronized" map is worse in general. But this solution can solve some cases when you want specifically "synchronized view" for your map.

SerCe
- 5,826
- 2
- 32
- 53
-
what is the difference between "synchronized map" and "synchronized view" – Elad Benda Mar 28 '16 at 10:18
-