What operations depend on the capacity of LinkedHashMap? Is any parallel version available?

I use ConcurrentHashMap as my data structure because mutiple threads will read and write at the same time. But I find that client code will also have to iterate through it often. So I took a look at the LinkedHashMap class, which gives the best iteration performance, and found this section in my java document:

A linked hash map has two parameters that affect its performance: initial power and load factor. They are defined exactly the same as for HashMap. Note, however, that the penalty for selecting an excessively high value for this class for the initial capacity is less severe than for HashMap, since the iteration time for this class is independent of capacity.

So the iteration is independent of capacity. What other operations depend on the initial capacity for LinkedHashMap or HashMap in general? Also, is there any parallel version of LinkedHashMap in recent versions of the JDK?

+2
source share
2 answers

With the exception of iterations, what other operations depend on the initial capacity for LinkedHashMap

As he claims, capacity is not important for iteration. Initial capacity is rarely of great importance, and if your load factor is reasonable, capacity will increase as needed.

There is also some parallel version of LinkedHashMap in recent versions of JDK

ConcurrentHashMap is the closest. If you need simultaneous access, use this.

0
source

If you look at the jashadoc HashMap, it states that:

Capacity is the number of buckets in the hash table, and the initial capacity is just the capacity at the time the hash table was created.

also that

When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is re-displayed (i.e. the internal data structures are rebuilt), so the hash table has about twice as many buckets.

LinkedHashMap is a HashTable based implementation and uses a linked list to maintain order. Initial capacity will not affect iteration performance. I believe that the only operation that will depend on capacity are insert operations, which can lead to a table reload. Map

0
source

Source: https://habr.com/ru/post/1432716/


All Articles