A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.
LinkedHashSet<String> hs = new LinkedHashSet<String>();
- Contains unique elements only like HashSet. It extends HashSet class and implements Set interface.
- Maintains insertion order.
Given below are the list of constructors supported by the LinkedHashSet:
- LinkedHashSet(): This constructor is used to create a default HashSet.
- LinkedHashSet(Collection C): Used in initializing the HashSet with the eleements of the collection C
- LinkedHashSet(int size): Used to intialize the size of the LinkedHashSet with the integer mentioned in the parameter.
- LinkedHashSet(int capacity, float fillRatio): Can be used to initialize both the capacity and the fill ratio, also called the load capacity of the LinkedHashSet with the arguments mentioned in the parameter. When the number of elements exceeds the capacity of the hash set is multiplied with the fill ratio thus expanding the capacity of the LinkedHashSet
Methods in LinkedHashSet:
- spliterator(): This method creates a late-binding and fail-fast Spliterator over the elements in this set.
- clear(): This method removes all of the elements from this set.
- contains(Object o): This method returns true if this set contains the specified element.
Below program explains the basic add and traversal operation of LinkedHashSet:
Size of LinkedHashSet=5 Original LinkedHashSet:[A, B, C, D, E] Removing D from LinkedHashSet: true Trying to Remove Z which is not present: false Checking if A is present=true Updated LinkedHashSet: [A, B, C, E]
Following is the difference between LinkedHashMap and LinekdHashSet:
Important : Keeping the insertion order in both LinkedHashmap and LinkedHashset have additional associated costs, both in terms of spending additional CPU cycles and needing more memory. If you do not need the insertion order maintained, it is recommended to use the lighter-weight HashSet and HashMap instead.
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- LinkedHashSet contains() method in Java with Examples
- LinkedHashSet clear() method in Java with Examples
- LinkedHashMap and LinkedHashSet in Java
- LinkedHashSet retainAll() method in Java with Example
- LinkedHashSet equals() method in Java with Example
- LinkedHashSet hashCode() method in Java with Example
- LinkedHashSet toString() method in Java with Example
- LinkedHashSet toArray() method in Java with Example
- LinkedHashSet containsAll() method in Java with Example
- LinkedHashSet removeAll() method in Java with Example
- Difference Between LinkedList and LinkedHashSet in Java
- LinkedHashSet toArray(T) method in Java with Example
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.byteValue() method in Java with Examples
Improved By : Chinmoy Lenka