Java hash value linkedHashSet

I know the following things related linkedHashSet

  • it supports insertion order
  • uses LinkedList to keep order
  • My question is how hashing happens.

I understand. If hashing is used, then the concept of bucketing comes in

However, checking the code in the JDK, it seems that the implementation of LinkedHashSet contains only the constructor and has no implementation, so I assume that all the logic happens in the HashSet?

  • why hashSet uses LinkedList by default?

Let me ask my question this way ... if the goal is to write a collection that

  • supports unique values
  • preserves the insertion order using the linked THEN list ... it can be easily done without Hashing ... maybe we can call this LinkedSet collection

saw a similar question what is the difference between a HashSet and LinkedHashSet , but not very useful

Let me know if I need to explain my question more

+5
source share
5 answers

False The implementation is LinkedHashSetreally in LinkedHashMap. (And the implementation is HashSetreally everything in HashMap. Le gasp!)

HashSet has no linked list.

It is possible to write a collection LinkedSetsupported by a linked list that retains unique elements - simply that its performance will be pretty crappy.

+1
source

"" . LinkedHashSet private-private HashSet, (LinkedHashMap) .

HashSet(int initialCapacity, float loadFactor, boolean dummy) {
    map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}

API , , , , "".

+1

, , HashSet, , . .

HashSet(int initialCapacity, float loadFactor, boolean dummy) {
    map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}

, keySet, LinkedHashSet, LinkedHashMap, HashMap, HashSet. java.util.LinkedList. , (Map.Entry<K,V>)

316    private static class Entry<K,V> extends HashMap.Entry<K,V> {
317        // These fields comprise the doubly linked list used for iteration.
318        Entry<K,V> before, after;
319
320        Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
321            super(hash, key, value, next);
322        }

, , . , , O (N), , .

+1

Set<Registeration> registerationSet = new LinkedHashSet<>();
registerationSet.add(new Registeration());

2.

  • hashCode
  • hashCode registerSet,
    • 3.1. , ,
    • 3,2. , /

A /

  • (3.1 ) .
+1

  • ? ( LinkedHashSet)

Java-...

  • HashSet, (, ), , - .
  • , , ( ).

, -, , LinkedList - , .

, ?

0

All Articles