Reputation: 1723
What is the best practice for creating a Map<>
object that will be access from multiple threads, but that will not be modified after initialization?
Would it be:
ConcurrentHashMap<>
objectHashMap<>
object and wrap this in a Collections#unmodifiableMap
call? Only retaining a reference to the unmodifiableMap
and not the original HashMap<>
.Thanks, Peter
Upvotes: 0
Views: 74
Reputation: 201439
I believe (of the presented options) option number 2 is likely to be far more efficient.
A CuncurrentHashMap
per the Javadoc, provides A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.
It has to perform work to provide that, just keep the immutable Map
instance and let multiple readers access it.
Upvotes: 2
Reputation: 7844
Why don't you try an ImmutableMap? Once created cannot be modified so doesn't matter how many threads try to read in parallel.
Upvotes: 2