LinkedHashSet and its Methods
Before we start with HashSet let us look at the Collection Hierarchy
Collection Hierarchy
The below diagram shows the Collection hierarchy consisting of classes and interfaces.
As per the scope of the blog our discussion will be limited to the LinkedHashSet class
What is LinkedHashSet ?
LinkedHashSet is a subclass of HashSet class which is used to store multiple elements. It is the implemented class of Set interface,Collection interface and Iterable interface. LinkedHashSet class comes from “java.utils” package.LinkedHashSet is not an index based data structure and all the datas are stored in accordance with the hashcode values. Duplicate elements cannot be stored in LinkedHashSet. LinkedHashSet allows only one null value to be stored in it. LinkedHashSet follow the order of insertion. LinkedHashSet elements can be iterated by only Iterator method. “HashTable” is the data structure of LinkedHashSet which means all the Collection elements of LinkedHashSet are stored using HashTable. There is also another data structure of LinkedHashSet called as doubly LinkedList which means all elements are doubly linked in an ordered way of HashSet. LinkedHashSet has a combination of HashTable and Doubly LinkedList so its underlying data structure is collectively called as hybrid data structure. LinkedHashSet is nether thread safe nor synchronized. Its initial capacity is 16 and its capacity is increased after load factor becomes 0.75.
Methods of LinkedHashSet class
Below are some of the methods of LinkedHashSet class:
Method Name | Description |
add(Object e) | This method is used to add a new element of any datatype into the LinkedHashSet class. |
addAll(Collection c) | This method is used to add all elements of different datatypes into the LinkedHashSet class. |
clear() | It is used to clear all the elements from the Set interface but it does not clear the LinkedHashSet class. |
contains(Object o) | This method is used to check the presence of a Collection element in the LinkedHashSet. It returns ‘true’ value if the element is present in LinkedHashSet and ‘false’ value if the element is not present in the LinkedHashSet. |
containsAll(Collection c) | This method is used to check if the LinkedHashSet contains all the collection elements. It returns ‘true’ value if all the elements are present in LinkedHashSet and ‘false’ value if any one of the element is not present in the LinkedHashSet. |
isEmpty() | This method checks if the given LinkedHashSet is empty |
iterator() | This method is used to iterate sequentially all the Collection elements present in a LinkedHashSet. |
remove(Object o) | This method is used to remove a specified collection element from the LinkedHashSet. |
removeAll(Collection c) | This method removes all those Collection elements from the current LinkedHashSet which are present in another LinkedHashSet. |
retainAll(Collection c) | This method retains all those elements from the current LinkedHashSet which are present in another LinkedHashSet and removes those elements which are present in current LinkedHashSet but not in another LinkedHashSet and vice versa. |
size() | This method is used to find out and display the number of Collection elements present in the LinkedHashSet |
toArray() | This method is used to convert all collection elements present in a LinkedHashSet into an array. |
toString() | This method is used to convert all collection elements present in a LinkedHashSet into a String. |
equals() | This method is used to check if two LinkedHashSets are equal. It returns ‘true’ value if elements as well as size of elements present in one LinkedHashSet is equal to the elements and its size present in another LinkedHashSet. If the elements and the size of elements present in one LinkedHashSet is not equal to the elements and its size present in another LinkedHashSet then it returns ‘false’ value. |
clone() | This method is used to create a shallow copy of the elements present in the LinkedHashSet class. |
1.Let us check the usage of add(Object e) method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
System.out.println(a1);
}
}
Output: [hello, c, 3.4, true, null, 3.55, Hiii]
Screenshot:
2. Let us check the usage of addAll(Collection c) method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
LinkedHashSet a2= new LinkedHashSet();
a2.addAll(a1);
System.out.println(a2);
}
}
Output: [hello, c, 3.4, true, null, 3.55, Hiii]
Screenshot:
3. Let us check the usage of clear() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.clear();
System.out.println(a1);
}
}
Output: []
Screenshot:
4. Let us check the usage of contains(Object o) method in LinkedHashSet class.
Code Snippet:
package hashSetClass;
import java.util.HashSet;
public class HashSetClass {
public static void main(String[] args) {
HashSet a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
System.out.println("The given LinkedHashSet contains: "+a1.contains("hello"));
System.out.println("The given LinkedHashSet contains: "+a1.contains("Harish"));
}
}
Output: The given LinkedHashSet contains: true
The given LinkedHashSet contains: false
Screenshot:
5. Let us check the usage of containsAll(Collection c) method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
LinkedHashSet a2= new LinkedHashSet();
a2.add("hello");
a2.add('c');
a2.add(3.4f);
a2.add(true);
System.out.println("Does a2 contains all elements of a1:"+a2.containsAll(a1));
System.out.println("Does a1 contains all elements of a2:"+a1.containsAll(a2));
}
}
Output:
Does a2 contains all elements of a1:false
Does a1 contains all elements of a2:true
Screenshot:
6. Let us check the usage of isEmpty() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
System.out.println("Is a1 LinkedHashSet empty?: "+a1.isEmpty());
LinkedHashSet a2= new LinkedHashSet();
System.out.println("Is a2 LinkedHashSet empty?: "+a2.isEmpty());
}
}
Output:
Is a1 LinkedHashSet empty?: false
Is a2 LinkedHashSet empty?: true
Screenshot:
7. Let us check the usage of iterator() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
Iterator a2= a1.iterator();
System.out.println("The collection elements values are: ");
while(a2.hasNext())
{
System.out.println(a2.next());
}
}
}
Output:
The collection elements values are:
hello
c
3.4
true
null
3.55
Hiii
Screenshot:
8. Let us check the usage of remove(Object o) method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
a1.remove("hello");
a1.remove(null);
System.out.println(a1);
}
}
Output: [c, 3.4, true, 3.55, Hiii]
Screenshot:
9. Let us check the usage of removeAll(Collection c) method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
LinkedHashSet a2= new LinkedHashSet();
a2.add(true);
a2.add('c');
a2.add("hello");
a2.add("hi");
a1.removeAll(a2);
System.out.println("Elements present in a1 after removing a2 elements from a1 are:"+a1);
}
}
Output:
Elements present in a1 after removing a2 elements from a1 are:[3.4, null, 3.55, Hiii]
Screenshot:
10. Let us check the usage of retainAll(Collection c) method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
LinkedHashSet a2= new LinkedHashSet();
a2.add(true);
a2.add('c');
a2.add("hello");
a2.add("hi");
a1.retainAll(a2);
System.out.println("Elements present in a1 after retaining a2 elements are:"+a1);
}
}
Output: Elements present in a1 after retaining a2 elements are:[hello, c, true]
Screenshot:
11. Let us check the usage of size() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
System.out.println("The size of the collection elements present in the LinkedHashSet is: "+a1.size());
}
}
Output: The size of the collection elements present in the LinkedHashSet is: 7
Screenshot:
12. Let us check the usage of toArray() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
Object a2[]= a1.toArray();
System.out.println("The list of array elements are:");
for(int i=0;i<=a2.length-1;i++)
{
System.out.println(a2[i]);
}
}
}
Output: The list of array elements are:
hello
c
3.4
true
null
3.55
Hiii
Screenshot:
13. Let us check the usage of toString() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
System.out.println(a1.toString());
}
}
Output: [hello, c, 3.4, true, null, 3.55, Hiii]
Screenshot:
14. Let us check the usage of equals() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
LinkedHashSet a2= new LinkedHashSet();
a2.add("hello");
a2.add('c');
a2.add(3.4f);
a2.add(true);
a2.add(null);
a2.add(3.55);
a2.add("Hiii");
LinkedHashSet a3= new LinkedHashSet();
a3.add("hello");
a3.add('c');
a3.add(3.4f);
a3.add(true);
System.out.println("Is LinkedHashSet a1 equal to a2 ?:"+ a1.equals(a2));
System.out.println("Is LinkedHashSet a2 equal to a3 ?:"+ a2.equals(a3));
}
}
Output: Is LinkedHashSet a1 equal to a2 ?:true
Is LinkedHashSet a2 equal to a3 ?:false
Screenshot:
15. Let us check the usage of clone() method in LinkedHashSet class.
Code Snippet:
package linkedHashSet;
import java.util.LinkedHashSet;
public class LinkedHashSetClass {
public static void main(String[] args) {
LinkedHashSet a1= new LinkedHashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.add("Hiii");
System.out.println("List of elements in LinkedHashSet class: "+a1);
LinkedHashSet clonea1= new LinkedHashSet();
clonea1=(LinkedHashSet)a1.clone();
System.out.println("List of elements after using clone method:"+ clonea1);
}
}
Output: List of elements in LinkedHashSet class: [hello, c, 3.4, true, null, 3.55, Hiii]
List of elements after using clone method:[hello, c, 3.4, true, null, 3.55, Hiii]
Screenshot:
Difference between HashSet and LinkedHashSet
SL NO: | HashSet | LinkedHashSet |
1 | It is the superclass of LinkedHashSet | It is the subclass of HashSet |
2 | Does not follow the order of insertion | Follows the order of insertion |
3 | The underlying data structure is HashTable only | The underlying data structure is a combination of HashTable and Doubly LinkedList |
Conclusion
LinkedHashSet class is used for handling all the Collection elements in an effective way. It is necessary to understand the usage of all the methods of LinkedHashSet for better handling of Collection elements. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process. By thoroughly understanding the LinkedHashSet class and its methods developers and testers can utitlise those concepts in their real life tasks.
Read my blogs on :
Consult Us