HashSet vs LinkedHashSet

后端 未结 10 2544
暗喜
暗喜 2020-11-28 18:20

What is the difference between them? I know that

A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all el

10条回答
  •  难免孤独
    2020-11-28 18:55

    LinkedHashSet's constructors invoke the following base class constructor:

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

    As you can see, the internal map is a LinkedHashMap. If you look inside LinkedHashMap, you'll discover the following field:

    private transient Entry header;
    

    This is the linked list in question.

提交回复
热议问题