Overview:
HashSet
is a widely-used class in the Java Collections Framework.Set
interface, which means it doesn’t allow duplicate elements.HashMap
, meaning it uses hashing to store elements.Characteristics:
HashSet
are unordered. There is no guarantee that the order of elements will remain constant over time.HashSet
allows one null
element.HashSet
, with an average time complexity of O(1) for operations like add
, remove
, and contains
.Internal Working:
HashSet
internally uses a HashMap
to store that element as a key.hashCode()
of the object to determine its position in the hash table. This allows for fast access.HashSet
checks both the hashCode
and equals
methods of the object.import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
// Creating a HashSet of String elements
HashSet<String> hashSet = new HashSet<>();
// Adding elements
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Cherry");
hashSet.add("Apple"); // Duplicate, will not be added
// Displaying elements (order not guaranteed)
System.out.println("HashSet Elements: " + hashSet);
// Checking if an element exists
System.out.println("Contains Banana: " + hashSet.contains("Banana"));
// Removing an element
hashSet.remove("Apple");
System.out.println("After Removing Apple: " + hashSet);
}
}
HashSet Elements: [Apple, Cherry, Banana]
Contains Banana: true
After Removing Apple: [Cherry, Banana]
Overview:
LinkedHashSet
is also an implementation of the Set
interface.HashSet
, but it maintains the order of insertion.HashSet
, LinkedHashSet
preserves the order in which elements were inserted.HashSet
due to the additional linked list overhead.null
element.LinkedHashSet
LinkedHashMap
: Similar to HashSet
, but internally backed by a LinkedHashMap
.HashSet
, LinkedHashSet
also stores elements in buckets based on their hash code.xxxxxxxxxx
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of String elements
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
// Adding elements
linkedHashSet.add("Apple");
linkedHashSet.add("Banana");
linkedHashSet.add("Cherry");
linkedHashSet.add("Apple"); // Duplicate, will not be added
// Displaying elements (order preserved)
System.out.println("LinkedHashSet Elements: " + linkedHashSet);
// Checking if an element exists
System.out.println("Contains Banana: " + linkedHashSet.contains("Banana"));
// Removing an element
linkedHashSet.remove("Apple");
System.out.println("After Removing Apple: " + linkedHashSet);
}
}
LinkedHashSet Elements: [Apple, Banana, Cherry]
Contains Banana: true
After Removing Apple: [Banana, Cherry]
Feature | HashSet | LinkedHashSet |
---|---|---|
Order of Elements | Unordered | Maintains insertion order |
Data Structure | Hashing | Hashing + Doubly Linked List |
Performance | Faster | Slightly slower |
Use Case | Best for quick access, when order doesn’t matter | Best when both quick access and order matter |
HashSet
:
HashSet
when order doesn’t matter, and you only need fast access to unique elements.LinkedHashSet
:
LinkedHashSet
when insertion order matters and you want the elements to be retrieved in the order they were added.