Java Data StructuresLesson 68Free preview
Java LinkedHashSet
This lesson is part of Java · All levels
Summary
LinkedHashSet is a set that remains in the order of insertion of the element when iterated. Internally, the elements are linked through a doubly linked list so that the sequence can be predicted. It's a little slower and more memory-intensive than a regular HashSet.
Key points
- Maintains the order of insertion during the for-each and the iterator
- Inherits all HashSet behavior: add and contains average O(1)
- Always use hashCode() and equals() to define uniqueness
- Choose when the order is important, but duplicate should not be.
- The constructor can set the initialCapacity and loadFactor
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.