LinkedHashSet is a class in Java that extends the HashSet class and provides a hash table implementation with a linked list running through it, thus combining the advantages of both data structures. The LinkedHashSet class maintains the order of elements in which they are inserted into the set, while also ensuring that the elements are unique. Here's a detailed explanation of the LinkedHashSet class, its constructors, and some of its methods:
* LinkedHashSet is a class that extends the HashSet class and implements the Set interface in Java.
* It stores elements in a hash table and also maintains a linked list of the elements in the order in which they were inserted into the set.
* It does not allow duplicate elements, and it provides constant-time performance for basic operations like add, remove, contains, and size.
1. LinkedHashSet(): This creates an empty LinkedHashSet with the default initial capacity (16) and default load factor (0.75).
2. LinkedHashSet(int initialCapacity): This creates an empty LinkedHashSet with the specified initial capacity and the default load factor (0.75).
3. LinkedHashSet(int initialCapacity, float loadFactor): This creates an empty LinkedHashSet with the specified initial capacity and load factor.
4. LinkedHashSet(Collection<? extends E> c): This creates a LinkedHashSet containing the elements of the specified collection, in the order in which they are returned by the collection's iterator.
1. add(E e): This adds the specified element to the LinkedHashSet if it is not already present.
2. remove(Object o): This removes the specified element from the LinkedHashSet if it is present.
3. contains(Object o): This returns true if the LinkedHashSet contains the specified element.
4. size(): This returns the number of elements in the LinkedHashSet.
5. clear(): This removes all elements from the LinkedHashSet.
6. isEmpty(): This returns true if the LinkedHashSet contains no elements.
7. iterator(): This returns an iterator over the elements in the LinkedHashSet, in the order in which they were inserted.
8. toArray(): This returns an array containing all elements in the LinkedHashSet, in the order in which they were inserted.
9. equals(Object o): This returns true if the specified object is also a LinkedHashSet and contains the same elements in the same order.
10. hashCode(): This returns the hash code value for the LinkedHashSet.
11. spliterator(): This returns a Spliterator over the elements in the LinkedHashSet, in the order in which they were inserted.
Here's an example of how to use the LinkedHashSet class in Java:
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
public static void main(String[] args) {
// create a new LinkedHashSet
LinkedHashSet<String> set = new LinkedHashSet<>();
// add some elements to the set
set.add("apple");
set.add("banana");
set.add("orange");
set.add("apple"); // adding a duplicate element
// print the set
System.out.println("LinkedHashSet: " + set);
// remove an element from the set
set.remove("banana");
// print the set after removing an element
System.out.println("LinkedHashSet after removing an element: " + set);
// check if the set contains an element
boolean contains = set.contains("orange");
System.out.println("LinkedHashSet contains orange: " + contains);
// get the size of the set
int size = set.size();
System.out.println("LinkedHashSet size: " + size);
// clear the set
set.clear();
System.out.println("LinkedHashSet after clearing: " + set);
}
}
Output:
LinkedHashSet: [apple, banana, orange]
LinkedHashSet after removing an element: [apple, orange]
LinkedHashSet contains orange: true
LinkedHashSet size: 2
LinkedHashSet after clearing: []
In the example, we create a new LinkedHashSet, add some elements to it, remove an element, check if the set contains an element, get the size of the set, and clear the set. We also print the contents of the set at various points in the program. Notice how the order of the elements is preserved in the LinkedHashSet, and how duplicates are not allowed.
The primary difference between LinkedHashSet and HashSet is that LinkedHashSet maintains the order of elements in which they were inserted into the set, while HashSet does not maintain any order. Therefore, if you need to preserve the order in which elements were inserted into the set, you should use LinkedHashSet. However, if you do not need to maintain order, HashSet may be a more efficient option, as it has a faster add and remove performance than LinkedHashSet.