HashSet vs LinkedHashSet

后端 未结 10 2543
暗喜
暗喜 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 19:09

    HashSet is unordered and unsorted Set.
    LinkedHashSet is the ordered version of HashSet.

    The only difference between HashSet and LinkedHashSet is that:
    LinkedHashSet maintains the insertion order.

    When we iterate through a HashSet, the order is unpredictable while it is predictable in case of LinkedHashSet.

    The reason for how LinkedHashSet maintains insertion order is that:
    The underlying used data structure is Doubly-Linked-List.

提交回复
热议问题