What We Are Learn On This Post
LinkedHashSet Class In Java: In The Series of Java Programming Language, we are learning about HashSet and TreeSet. In this post, we are going to learn about LinkedHashSet, which also 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 Set Interface.
- Duplicate Values are not allowed inside LinkedHashSet Class.
- One Null Element is allowed inside LinkedHashSet.
- Elements are stored in an insertion order that means the elements get stored in the same order in which they have added into the LinkedHashSet.
- This class provides consistent performance for an operation like add, remove, contain, and size.
- LinkedHashSet is not threaded safe or not synchronized, which means when multiple threads are acting on this, and then the result is un-deterministic. 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): 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
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 to Remove LinkedHashSet Elements in Java With 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 to Print Iterate LinkedHashSet Elements In Java With 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 to Search LinkedHashSet Value In Java With 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
Ref: Article
Leave a Reply