I have roughly looked at the source code here. LinkedHashSet and HashSet are maintained by LinkedHashMap and HashMap, and the essence of the data structure of LinkedHashMap is that the same set of data uses different arrangements, which can be understood as:
5, 3, 2, 4, 1 is the arrangement of the array, 1, 2, 3, 4, 5 is the arrangement of the linked list. The parameter initialCapacity is related to the array, the linked list does not need to be expanded, it will only increase with more data. So it is concluded that the linked list has nothing to do with the size that affects the initialCapacity parameter.
As for this question, I think it's two different ideas, **map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
The idea is to not put the value after the collection is full. The most used scenario is the get value, because it can be found that the HashMap
will expand again when the value is set again with this parameter. **
**super(Math.max(2*c.size(), 11), .75f, true);
is the opposite of the idea, it has reserved space after the filled value is put in, so that The put value will not automatically expand immediately. So why 2x? The answer is that the underlying principle of hashMap expansion is to expand by 2 times. **
A linked hash set has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashSet. Note, however, that the penalty for choosing an excessively high value for initial capacity is less severe for this class than for HashSet, as iteration times for this class are unaffected by capacity.
-- docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.htmlHashSet
uses the greater of4/3
the size or16
, whileLinkedHashSet
uses the greater of twice the size, or11
. Both use a load factor of0.75f
LinkedHashSet
than a plainHashSet
, as the former has a linked list running through it, which might also have to be refactored/recalculated. By making the initial capacity greater, we might avoid exceeding the initial capacity for some typical use cases.