LinkedHashSet in Java with Example - Javastudypoint

Monday, March 4, 2019

LinkedHashSet in Java with Example

LinkedHashSet in Java is a class which is present in java.util package. It is the child class of HashSet and implements the set interface.

Points to remember about LinkedHashSet class in Java:
1. LinkedHashSet class contains unique elements.
2. It allows null value.
3. The underlying data structure for LinkedHashSet is both a hash table and linked list.
4. In LinkedHashSet class, insertion order is preserved.
LinkedHashSet in Java with Example


Difference between HashSet and LinkedHashSet:

LinkedHashSet is similar to HashSet except the below-mentioned differences.

1. In HashSet insertion order is not preserved it means the elements will not be returned in the same order in which we are inserted, while in case of LinkedHashSet insertion order must be preserved.

2. The underlying data structure for HashSet is a hash table while the underlying data structure for LinkedHashSet is both hash table and Linked List.

3. HashSet class came in Java 1.2 version while LinkedHashSet class came in Java 1.4 version.

Constructors in Java LinkedHashSet Class:

There are 4 constructors are available in Java HashSet class, which are described below:

1. LinkedHashSet(): This constructor creates a default linked hash set.

2. LinkedHashSet(int capacity): It is used to initialize the capacity of the linked hash set to the given capacity.

3. LinkedHashSet(int capacity, float loadFactor): This constructor is used to initialize the capacity of the linked hash set to the given capacity and the given load factor.

4. LinkedHashSet(Collection<? extends E> c): It is used to initializes the linked hash set by using the elements of collection c.

Methods in Java LinkedHashSet Class:

LinkedHashSet class in Java is the child class of HashSet class. It defines the same methods which are available in HashSet class and it doesn't add methods of its own.


Java LinkedHashSet Example:


import java.util.*;

class linkedHashSetExample



public static void main(String args[]) 

{

//creating a hash set 

LinkedHashSet<String> lhset = new LinkedHashSet<String>();


 //adding elements in linked hash set 

lhset.add("Amit"); 

lhset.add("Sumit"); 

lhset.add("Rohit"); 

lhset.add("Virat"); 

lhset.add("Vijay"); 

lhset.add("Ajay");


 //adding duplicate values

lhset.add("Rohit");

lhset.add("Sumit");


 //find size of linked hash set

System.out.println("Size of the linked hash set is: "+lhset.size());


 //displaying original linked hash set

System.out.println("The original linked hash set is: " +lhset);


 //removing element from linked hash set

System.out.println("Removing(Rohit) from the linked hash set: " +lhset.remove("Rohit"));


 //checking specified element present or not

System.out.println("(Sumit)present in the linked hash set or not: " +lhset.contains("Sumit"));


 //displaying updated linked hash set

System.out.println("Finally, the updated linked hash set is: "+lhset);



}


Output:

Size of the linked hash set is: 6
The original linked hash set is: [Amit, Sumit, Rohit, Virat, Vijay, Ajay]
Removing(Rohit) from the linked hash set: true
(Sumit) present in the linked hash set or not: true
Finally, the updated linked hash set is: [Amit, Sumit, Virat, Vijay, Ajay]





You may also like:
What is Collection framework in Java?

How to add and remove elements in ArrayList?
How to implement a LinkedList data structure in Java?
Difference between ArrayList vs Vector in Java?
How to implement a Stack data structure in Java?
Inheritance in Java OOPs concept with the example
JSP Standard Tag Library- JSTL Tutorial
How to manage session in Java?
Difference between Type1, Type2, Type3, and Type4 driver in Java?


1 comment:

  1. Thanks for sharing valuable information.it such very helpful for me.Keep it up.

    ReplyDelete