HashSet

Overview:

  • HashSet is a widely-used class in the Java Collections Framework.
  • It implements the Set interface, which means it doesn’t allow duplicate elements.
  • It is backed by a HashMap, meaning it uses hashing to store elements.

Characteristics:

  • No Duplicates: HashSet does not allow duplicate elements. If you try to add a duplicate element, it will be ignored.
  • Order Not Maintained: Elements in a HashSet are unordered. There is no guarantee that the order of elements will remain constant over time.
  • Null Elements: HashSet allows one null element.
  • Fast Access: Due to hashing, accessing elements is generally faster in a HashSet, with an average time complexity of O(1) for operations like add, remove, and contains.

Internal Working:

  • When an element is added, HashSet internally uses a HashMap to store that element as a key.
  • Hashing involves using the hashCode() of the object to determine its position in the hash table. This allows for fast access.
  • To prevent duplicate entries, HashSet checks both the hashCode and equals methods of the object.
  • Hashing: Elements are stored in buckets based on their hash code. When an element is added, its hash code is calculated, and it is placed in a specific bucket.
  • Collision Handling: Uses Separate Chaining (linked lists or balanced trees within buckets) to handle hash collisions.
Code Example
Output
HashSet Elements: [Apple, Cherry, Banana]
Contains Banana: true
After Removing Apple: [Cherry, Banana]

LinkedHashSet

Overview:

  • LinkedHashSet is also an implementation of the Set interface.
  • It is a subclass of HashSet, but it maintains the order of insertion.

Characteristics

  • Maintains Insertion Order: Unlike HashSet, LinkedHashSet preserves the order in which elements were inserted.
  • Doubly-Linked List: Internally maintains a doubly-linked list that links all elements in insertion order.
  • Fast Access: Provides constant time performance for basic operations, though slightly slower than HashSet due to the additional linked list overhead.
  • Null Element: Allows one null element.

Internal Structure of LinkedHashSet

  • Backed by a LinkedHashMap: Similar to HashSet, but internally backed by a LinkedHashMap.
  • Doubly-Linked List: Maintains a doubly-linked list that keeps track of the insertion order.
  • Hashing and Buckets: Like HashSet, LinkedHashSet also stores elements in buckets based on their hash code.
Code Example
Output
LinkedHashSet Elements: [Apple, Banana, Cherry]
Contains Banana: true
After Removing Apple: [Banana, Cherry]

Key Differences between HashSet and LinkedHashSet

FeatureHashSetLinkedHashSet
Order of ElementsUnorderedMaintains insertion order
Data StructureHashingHashing + Doubly Linked List
PerformanceFasterSlightly slower
Use CaseBest for quick access, when order doesn’t matterBest when both quick access and order matter

Use Cases and When to Use Which

HashSet:

  • Use HashSet when order doesn’t matter, and you only need fast access to unique elements.
  • Examples: Storing unique IDs, tags, or other unordered items where insertion order isn’t important.

LinkedHashSet:

  • Use LinkedHashSet when insertion order matters and you want the elements to be retrieved in the order they were added.
  • Examples: Maintaining unique user inputs in the order they were entered, or a history of operations in a specific sequence.