LinkedHashSet Class In Java: In the Java programming language series, we learned about HashSet and TreeSet. In this post, we will learn about LinkedHashSet, which extends the HashSet Class and implements the Set Interface. LinkedHashSet stores the elements in the same order in which they are inserted.
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable { //implementation }
LinkedHashSet Syntax
LinkedHashSet<String> hs = new LinkedHashSet<String>();
Important Points about LinkedHashSet Class
- It Extends the HashSet class and implements the Set Interface.
- Duplicate Values are not allowed inside the LinkedHashSet Class.
- One Null Element is allowed inside LinkedHashSet.
- Elements are stored in an insertion order, which means the elements get stored in the same order in which they have been added to the LinkedHashSet.
- This class consistently performs add, remove, contain, and size operations.
- LinkedHashSet is not threaded safely or synchronized, which means the result is undeterministic when multiple threads act on this. But externally, we can make it synchronized with Collections.synchronizedSet(new LinkedHashSet()).
- LinkedHashSet implements Serializable and Cloneable interfaces.
LinkedHashSet Class Constructors
- LinkedHashSet(): This constructor is used to create a default HashSet.
- LinkedHashSet(Collection C): Used in initializing the HashSet with the elements of the Collection C
- LinkedHashSet(int size): Used to initialize the size of the LinkedHashSet with the integer mentioned in the parameter.
- LinkedHashSet(int capacity, float fillRatio): This 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, it is multiplied with the fill ratio, thus expanding the capacity of the LinkedHashSet.
LinkedHashSet Class Methods
- 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.
How to Find The LinkedHashSet Size & Check Empty In Java?
package com.java.Softwaretestingblog; import java.util.LinkedHashSet; public class LkdHashSetOperations { public static void main(String[] args) { // TODO Auto-generated method stub LinkedHashSet<String> lhs = new LinkedHashSet<String>(); //add elements to HashSet lhs.add("first"); lhs.add("second"); lhs.add("third"); System.out.println(lhs); System.out.println("LinkedHashSet size: "+lhs.size()); System.out.println("Is LinkedHashSet emplty? : "+lhs.isEmpty()); } }
Output:
[first, second, third] LinkedHashSet size: 3 Is LinkedHashSet emplty? : false
How do you remove LinkedHashSet elements in Java with an example?
package com.java.Softwaretestingblog; import java.util.LinkedHashSet; public class LinkedHashSetClear { public static void main(String[] args) { // TODO Auto-generated method stub LinkedHashSet<String> lhs = new LinkedHashSet<String>(); //add elements to HashSet lhs.add("first"); lhs.add("second"); lhs.add("third"); System.out.println("My LinkedHashSet content:"); System.out.println(lhs); System.out.println("Clearing LinkedHashSet:"); lhs.clear(); System.out.println("Content After clear:"); System.out.println(lhs); } }
Output:
[first, second, third] Does set contains 'first'? true
How do you print iterate LinkedHashSet elements in Java with an example?
package com.java.Softwaretestingblog; import java.util.Iterator; import java.util.LinkedHashSet; public class LinkedHashSetIteratorExample { public static void main(String[] args) { // TODO Auto-generated method stub LinkedHashSet<String> lhs = new LinkedHashSet<String>(); //add elements to HashSet lhs.add("first"); lhs.add("second"); lhs.add("third"); Iterator itr = lhs.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
Output:
first second third
Search LinkedHashSet In Java Collection: How do you search LinkedHashSet Value In Java with an example?
package com.java.Softwaretestingblog; import java.util.LinkedHashSet; public class LinkedHashSetSearchExample { public static void main(String[] args) { // TODO Auto-generated method stub LinkedHashSet<String> lhs = new LinkedHashSet<String>(); //add elements to HashSet lhs.add("first"); lhs.add("second"); lhs.add("third"); System.out.println(lhs); System.out.println("Does set contains 'first'? "+lhs.contains("first")); } }
Output:
[first, second, third] Does set contains 'first'? true