2.3.3 LinkedHashSet Explained
The LinkedHashSet
class in Java is a part of the Java Collections Framework and extends the HashSet
class. It maintains a doubly-linked list running through all of its entries, ensuring that the insertion order of elements is preserved. This makes LinkedHashSet
particularly useful when the order of insertion is important.
Key Concepts
1. Insertion Order Preservation
Unlike HashSet
, which does not guarantee any order of elements, LinkedHashSet
maintains the order in which elements are inserted. This means that when you iterate through a LinkedHashSet
, the elements will be returned in the order they were added.
2. Hash Table and Linked List Structure
LinkedHashSet
combines the properties of a hash table for fast lookup and a linked list for maintaining insertion order. This dual structure ensures that elements can be accessed quickly and that their order of insertion is preserved.
3. No Duplicate Elements
Similar to HashSet
, LinkedHashSet
does not allow duplicate elements. If you try to add an element that already exists in the set, the new element will not be added, and the set will remain unchanged.
Explanation and Examples
Insertion Order Preservation Example
Consider the following code snippet that demonstrates the preservation of insertion order in a LinkedHashSet
:
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add("Apple"); linkedHashSet.add("Banana"); linkedHashSet.add("Cherry"); // Output: [Apple, Banana, Cherry] System.out.println(linkedHashSet);
In this example, the elements are printed in the order they were added, demonstrating the preservation of insertion order.
Hash Table and Linked List Structure Example
Consider the following code snippet that demonstrates the combination of hash table and linked list in a LinkedHashSet
:
LinkedHashSet<Integer> numbers = new LinkedHashSet<>(); numbers.add(10); numbers.add(20); numbers.add(30); // Output: [10, 20, 30] System.out.println(numbers); // Fast lookup System.out.println(numbers.contains(20)); // Output: true
In this example, the elements are stored in a hash table for fast lookup, and their insertion order is maintained using a linked list.
No Duplicate Elements Example
Consider the following code snippet that demonstrates the prevention of duplicate elements in a LinkedHashSet
:
LinkedHashSet<String> fruits = new LinkedHashSet<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Apple"); // Duplicate, will not be added // Output: [Apple, Banana] System.out.println(fruits);
In this example, the duplicate element "Apple" is not added to the set, demonstrating the prevention of duplicate elements.
Analogies
Think of a LinkedHashSet
as a unique collection of items stored in a specific order, similar to a line of people waiting for a bus. Each person (element) has a unique identity, and the order in which they joined the line (insertion order) is preserved. If someone tries to join the line again, they are not allowed to cut in front of others.
By mastering the LinkedHashSet
class, you can efficiently manage and manipulate data in scenarios where both uniqueness and insertion order are important, making your Java SE 11 applications more organized and predictable.