Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to take static snapshot of ConcurrentHashMap?

Java doc says that return values of method values() and entrySet() are backed by the map. So changes to the map are reflected in the set and vice versa. I don't want this to happen to my static copy. Essentially, I want lots of concurrent operations to be done on my DS. But for some cases I want to iterate over its static snapshot. I want to iterate over static snapshot, as I am assuming iterating over static snapshot will be faster as compared to a version which is being updated concurrently.

like image 208
PHcoDer Avatar asked Jun 13 '16 05:06

PHcoDer


1 Answers

Just make a copy, and it wont be changed.

Set<K> keySetCopy = new HashSet<>(map.keySet());
List<V> valuesCopy = new ArrayList<>(map.values());

All collection implementations have a copy constructor which will copy the entire data of the supplied collection to the newly created one, without being backed by the original.

Note: this won't work with entrySet(), as the actual Map Entries will still "belong" to the original Map and changes to the original entries will be reflected in your copies. In case you need the entrySet(), you should copy the entire Map first, with the same technique.

Set<Entry<K,V>> entrySetCopy = new HashMap<>(map).entrySet();

Note that all of these will require a full iteration ONCE (in the constructor) and will only then be static snapshots. There is no way around this limitation, to my knowledge.

like image 78
Sean Patrick Floyd Avatar answered Oct 22 '22 07:10

Sean Patrick Floyd