LinkedHashSet in Java
LinkedHashSet in Java
1. What is LinkedHashSet?
LinkedHashSet is a part of the Java Collection Framework (java.util package).
It extends HashSet and implements the Set interface.
Maintains insertion order, unlike HashSet.
Does not allow duplicate elements.
Uses hash table + linked list to store elements.
// Adding elements
cities.add("New York");
cities.add("London");
cities.add("Paris");
cities.add("Tokyo");
cities.add("London"); // Duplicate, won't be added
// Removing an element
cities.remove("Tokyo");
System.out.println("After removing 'Tokyo': " + cities);
// Checking size
System.out.println("Size of LinkedHashSet: " + cities.size());
Method Description