-2

Is there any way to convert java8 Map<K,V> to ConcurrentMap<K,V> other than iterating manually over all entities?

Som Bhattacharyya
  • 3,972
  • 35
  • 54
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

3 Answers3

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 .

JavaDoc

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