HashSet and LinkedHashSet in Java with Examples


HashSet and LinkedHashSet are two popular implementations of the Set interface in Java's Collections Framework. These data structures play a crucial role in managing unique elements, providing efficient operations for adding, removing, and checking the existence of elements. In this article, we will explore HashSet and LinkedHashSet in Java, understanding their concepts, differences, and practical usage. We'll also provide complete Java code snippets, including a main function, to demonstrate these collections' capabilities.

 

HashSet: Unordered Collection of Unique Elements

HashSet is an unordered collection that does not allow duplicate elements. It uses a hash table for storage and provides constant-time average complexity for basic operations like add, remove, and contains. HashSet is suitable for scenarios where you need to ensure the uniqueness of elements without preserving their order.

 

Creating a HashSet:

You can create a HashSet in Java like this:

 

import java.util.HashSet;

HashSet<String> names = new HashSet<>();

 

Adding and Removing Elements:

To add elements to a HashSet, use the add method:

 

names.add("Alice");
names.add("Bob");

 

To remove elements, use the remove method:

 

names.remove("Alice");

 

Checking for Element Existence:

You can check if an element exists in a HashSet using the contains method:

 

boolean containsAlice = names.contains("Alice");

 

LinkedHashSet: Ordered Collection of Unique Elements

LinkedHashSet, like HashSet, does not allow duplicate elements, but it also maintains the order of elements as they were inserted. It uses a hash table with a linked list to achieve this. LinkedHashSet is useful when you need both uniqueness and order preservation.

 

Creating a LinkedHashSet:

You can create a LinkedHashSet in Java like this:

 

import java.util.LinkedHashSet;

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

 

Adding and Removing Elements:

Adding and removing elements from a LinkedHashSet works the same way as with HashSet.

 

Preserving Insertion Order:

The primary advantage of LinkedHashSet is that it preserves the order of elements:

 

names.add("Alice");
names.add("Bob");
names.add("Charlie");

// The elements are guaranteed to be in the order they were added
// Alice, Bob, Charlie

 

Example: Using HashSet and LinkedHashSet

Let's create a Java program that demonstrates the usage of HashSet and LinkedHashSet:

 

import java.util.HashSet;
import java.util.LinkedHashSet;

public class SetExample {
   public static void main(String[] args) {
       // Using HashSet
       HashSet<String> hashSet = new HashSet<>();
       hashSet.add("Apple");
       hashSet.add("Banana");
       hashSet.add("Cherry");
       hashSet.add("Apple"); // Duplicate element

       System.out.println("HashSet:");
       for (String fruit : hashSet) {
           System.out.println(fruit);
       }

       // Using LinkedHashSet
       LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
       linkedHashSet.add("Orange");
       linkedHashSet.add("Pear");
       linkedHashSet.add("Grape");
       linkedHashSet.add("Orange"); // Duplicate element

       System.out.println("\nLinkedHashSet:");
       for (String fruit : linkedHashSet) {
           System.out.println(fruit);
       }
   }
}

 

Output :
 

HashSet:
Banana
Cherry
Apple

LinkedHashSet:
Orange
Pear
Grape

 

Summary

HashSet and LinkedHashSet are essential collections in Java for managing unique elements. While HashSet provides constant-time operations for basic set operations, LinkedHashSet additionally preserves the order of insertion. Understanding the differences between these collections and their practical usage can greatly benefit Java developers when dealing with data uniqueness and order preservation in their applications.



Thanks for feedback.



Read More....
Java Exception Handling
HashMap in Java
Inner classes in Java
Java Collections Framework
Java Data Types and Variables - Declaration and Initialization