What is LinkedHashSet in Java?
- In Java, LinkedHashSet could be a class that executes the Set interface, which represents a collection of special components.
- It is a portion of the Java Collections Framework and gives a combination of the features of both a HashSet and a LinkedList.
- Like HashSet, a LinkedHashSet does not permit duplicate components, but not at all like HashSet, it maintains the insertion order of components.
- This makes it a valuable data structure once you require a set that ensures both uniqueness and the order in which elements were added.
- LinkedHashSet is portion of the java.util package and is regularly utilized after you got to store components in a set with unsurprising iteration order whereas still profiting from quick execution for fundamental set operations like add, remove, and contains.
Key Highlights of LinkedHashSet in Java
1.Unique Elements:
- Like other Set usage, a LinkedHashSet does not permit duplicate components. Any attempt to add a duplicate component is overlooked.
2.Insertion Order:
- The foremost imperative highlight that recognizes LinkedHashSet from other Set executions (such as HashSet) is that it keeps up the arrange of addition.
- When iterating over a LinkedHashSet, elements will be returned within the same arrange in which they were added.
3.Constant-Time Operations:
- Like HashSet, LinkedHashSet offers O(1) time complexity for essential operations like add(), remove(), and contains(), expecting a great hash function and low collision rate.
4.Null Elements:
- LinkedHashSet permits at most one invalid component, comparative to HashSet.
5.Not Synchronized:
- By default, LinkedHashSet isn’t synchronized. On the off chance that string security is required, you’ll either manually synchronize the set or utilize Collections.synchronizedSet().
How Does LinkedHashSet in java Work?
- Inside, LinkedHashSet uses a combination of a hash table (like HashSet) and a doubly-linked list to preserve the insertion order of elements.
- Each component within the set is linked to its predecessor and successor, which permits the set to preserve the order in which components were inserted.
Types of LinkedHashSet Implementations
- LinkedHashSet may be a concrete class, and it doesn’t have coordinate subclasses or elective varieties inside the standard Java Collections Framework.
- In any case, it’s imperative to distinguish LinkedHashSet from other Set executions, such as HashSet and TreeSet, in terms of functionality:
1.LinkedHashSet:
- Description: A set usage that keeps up insertion order whereas providing the one of a kind properties of a set.
- Use Case: Once you require a set with unsurprising iteration order and constant-time execution for adding, removing, and checking components.
- Example:
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
public static void main(String[] args) {
// Create a LinkedHashSet to store String elements
LinkedHashSet<String> countries = new LinkedHashSet<>();
// Add elements to the set
countries.add(“USA”);
countries.add(“Canada”);
countries.add(“Germany”);
// Print the LinkedHashSet (elements will be in insertion order)
System.out.println(countries); // Output: [USA, Canada, Germany]
// Check if an element exists
System.out.println(countries.contains(“USA”)); // true
// Remove an element
countries.remove(“Canada”);
System.out.println(countries); //
Key Operations on LinkedHashSet in Java
- The LinkedHashSet class offers a few valuable methods for managing its components.
- These methods permit you to perform essential set operations whereas profiting from the insertion order.
- Below are a few key operations:

1.add(E e):
- Adds the required component to the set. If the component as of now exists within the set, it won’t be added (since sets don’t permit duplicates).
- Time Complexity: O(1) within the average case.
- Example:
linkedHashSet.add("USA"); // Adds "USA" to the set.
linkedHashSet.add("Canada"); // Adds "Canada" to the set.
linkedHashSet.add("USA"); // This will not be added as "USA" is already in the set.
2.remove(Object o):
- Removes the required component from the set.
- Time Complexity: O(1) for average cases.
- Example:
linkedHashSet.remove("Germany"); // Removes "Germany" from the set.
3.contains(Object o):
- Checks in case the required element exists within the set.
- Time Complexity: O(1) on average.
- Example:
boolean exists = linkedHashSet.contains("USA"); // Checks if "USA" exists in the set.
System.out.println(exists); // Output: true
4.size():
- Returns the number of components within the set.
- Time Complexity: O(1).
- Example:
int size = linkedHashSet.size(); // Returns the number of elements in the set.
System.out.println(size); // Output: 3
5.clear():
- Removes all components from the set.
- Time Complexity: O(n), where n is the number of components within the set.
- Example:
linkedHashSet.clear(); // Clears all elements from the set.
6.isEmpty():
- Checks in case the set is empty.
- Time Complexity: O(1).
- Example:
boolean empty = linkedHashSet.isEmpty(); // Returns true if the set is empty.
7.iterator():
- Returns an iterator to navigate the set. The iterator emphasizes within the insertion order of the components.
- Time Complexity: O(1) for each component.
- Example:
Iterator<String> iterator = linkedHashSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next()); // Iterates over elements in insertion order.
}
Performance Considerations
1.Time Complexity:
- add(), remove(), and contains() operations regularly have O(1) time complexity in average cases.
- In any case, due to the inner doubly-linked list, keeping up the insertion order contains a slight overhead compared to HashSet, in spite of the fact that it still performs well.
- Iteration: Repeating through the set takes O(n) time, where n is the number of components within the set.
2.Memory Usage:
- LinkedHashSet uses more memory than HashSet because it stores both a hash table and a doubly linked list to preserve the order of insertion.
- In this manner, in case memory utilization could be a concern, a HashSet might be more suitable.