Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is LinkedHashSet in Java? Understand with examples

Published on Jul 04,2019 4.3K Views

11 / 22 Blog from Java Collections

A Set is a collection which does not allow any duplicate elements. The most commonly used implementations of the set interface are HashSet, TreeSet and LinkedHashSet. In this article, we are going to explore one of these implementations: LinkedHashSet in Java.

Listed below are topics discussed in this article:

What is LinkedHashSet?

LinkedHashSet is a collection interface framework in Java. Basically, it is the child class or derived class of superclass HashSet. It differs from HashSet in the following ways:

  1. The insertion order of elements is preserved during the creation of a LinkedHashSet.
  2. An underlying data structure is the hybrid of Hash Table (in HashSet) & Linked List.
  3. Duplicates are not allowed in LinkedHashSet.

When we traverse through a LinkedHashSet via an iterating agent, the elements will be returned back in the same order as they were inserted. The flowchart provided below explains that the interface Set implements class LinkedHashSet in Java

Hierarchy - LinkedHashSet in Java - Edureka

At the beginning of this article, we discussed how LinkedHashSet differs from HashSet. Let’s take a look at some example programs to understand how LinkedHashSet is different.

Order of Insertion

Here’s an example program of LinkedHashSet demonstrating if the insertion order is preserved or not.

import java.util.*;
public class Method1 {
   public static void main(String args[]) {
       LinkedHashSet hs=new LinkedHashSet(); 
       // Adding elements to the LinkedHashSet
       hs.add("E");
       hs.add("d");
       hs.add("u");
       hs.add("r");
       hs.add("e");
       hs.add("k");
       hs.add("a");
       // Displaying the updated LinkedHashSet
       System.out.println("Updated LinkedHashSet: "+ hs);
   }
}

Output

Updated LinkedHashSet: [E, d, u, r, e, k, a]

The output clearly shows that the insertion order is preserved in LinkedHashSet in contrary to the HashSet Class.

Redundancy

In LinkedHashSet duplicate elements are not allowed. Let us look at an illustration to check whether this is true or not.

import java.util.*;
public class Method1 {
   public static void main(String args[]) {
       LinkedHashSet hs=new LinkedHashSet(); 
       // Adding elements to the LinkedHashSet
       hs.add("E");
       hs.add("E");//addition of duplicate elements
       hs.add("d");
       hs.add("u");
       hs.add("r");
       hs.add("e");
       hs.add("k");
       hs.add("a");
       hs.add("a");//addition of duplicate elements
       // Displaying the LinkedHashSet 
       System.out.println("LinkedHashSet Contains : "+ hs); }
}

Output

LinkedHashSet Contains : [E, d, u, r, e, k, a]

So keeping in mind that duplicates are not allowed and the insertion order is preserved LinkedHashSet is widely used in building cache-based applications. Well, let’s get to the next topic of this ‘LinkedHashList in Java’ article.

Java LinkedHashSet Examples

Here are some example programs demonstrating the use of LinkedHashSet in Java.

Calculating Size and Searching for an Element in the LinkedHashSet

import java.util.*;
public class Example1 {
   public static void main(String args[]) {
       LinkedHashSet hs=new LinkedHashSet(); 
       // Adding elements to the LinkedHashSet
       hs.add("E");
       hs.add("d");
       hs.add("u");
       hs.add("r");
       hs.add("e");
       hs.add("k");
       hs.add("a");
       // Getting the size of the LinkedHashSet
       System.out.println("The size of the LinkedHashSet is  "+ hs.size());
       // Checking whether the LinkedHashSet  contains an element or not
       System.out.println("Is B Present in the LinkedHashSet?:  "+ hs.contains("B")); 
       // Checking whether the LinkedHashSet  contains an element or not
       System.out.println("Is E Present in the LinkedHashSet?:  "+ hs.contains("E"));
} }

Output

The size of the LinkedHashSet is 7
Is B Present in the LinkedHashSet?: false
Is E Present in the LinkedHashSet?: true

If the element is present in the HashSet program returns true and in case the element is not found program returns false

Removing an Element from the LinkedHashSet

import java.util.*;
public class Example2 {
   public static void main(String args[]) {
       LinkedHashSet hs=new LinkedHashSet(); 
       // Adding elements to the LinkedHashSet
       hs.add("E");
       hs.add("d");
       hs.add("u");
       hs.add("r");
       hs.add("e");
       hs.add("k");
       hs.add("a");
       System.out.println("Original LinkedHashSet: " + hs);
       // Removing element e from the LinkedHashSet
       System.out.println("Removal Status: "+hs.remove("e"));
       // Displaying the updated LinkedHashSet
       System.out.println("Updated LinkedHashSet: "+ hs); } }

Output

Original LinkedHashSet: [E, d, u, r, e, k, a]
Removal Status: true
Updated LinkedHashSet: [E, d, u, r, k, a]

As you can see, the order remains unchanged and the element is successfully removed from the set.  

This brings us to the end of this ‘LinkedHashSet in Java’ article. I hope the Java LinkedHashSet class examples that we discussed here will help you in getting started with LinkedHashSet programming in Java.

Make sure you practice as much as possible and revert your experience.  

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this ‘LinkedHashSet in Java’ article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is LinkedHashSet in Java? Understand with examples

edureka.co