Open In App

Java Program To Remove All The Duplicate Entries From The Collection

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

As we know that the HashSet contains only unique elements, ie no duplicate entries are allowed, and since our aim is to remove the duplicate entries from the collection, so for removing all the duplicate entries from the collection, we will use HashSet.The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. The class also offers constant time performance for the basic operations like add, remove, contains, and size assuming that the hash function disperses the elements properly among the buckets. HashSet is generally used to keep a check on whether an element is present in a list or not.

Note: Since we are using HashSet so the insertion order will not be preserved and every time we run the code we are going to get some different output (order of elements will be different). So If we want to preserve the order of elements while inserting, then we are supposed to use LinkedHashSet.

There are basically two methods to remove the duplicate entries from the collection:

  1. Using HashSet
  2. Using LinkHashSet

Now Let’s see the implementation using the java program to remove the duplicate entries by using both the methods one by one:-

1. Using HashSet

Java




// Java Program to remove the duplicate entries from
// collection using HashSet
  
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
  
class GFG {
    public static void main(String[] args)
    {
        // making the collection object
        Collection<String> collection = new ArrayList<>();
  
        // adding the elements to the collection
        collection.add("Geeks");
        collection.add("For");
        collection.add("Geeks");
        collection.add("Internship");
        collection.add("Internship");
        collection.add("2021");
        collection.add("2021");
  
        // Displaying the collection elements
        System.out.println(
            "Displaying the initial collection\n");
        System.out.println(collection);
  
        // HashSEt for deleting duplicate entries
        // in the collection by passing collection
        // in the constructor of the HashSet
        HashSet<String> hashSet = new HashSet<>(collection);
  
        // Displaying the HashSet
        System.out.println("\nDisplaying the HashSet\n");
        System.out.println(hashSet);
  
        // clearing all the elements of the collection
        collection.clear();
  
        // adding all the elements back
        // to the collection from HashSet
        collection.addAll(hashSet);
  
        // Displaying the collection
        System.out.println(
            "\nDisplaying the collection after deleting duplicates entries\n");
        System.out.println(collection);
    }
}


Output: 

Displaying the initial collection

[Geeks, For, Geeks, Internship, Internship, 2021, 2021]

Displaying the HashSet

[Geeks, For, 2021, Internship]

Displaying the collection after deleting duplicates entries

[Geeks, For, 2021, Internship]

2.  Using LinkedHashSet

Java




// Java Program to remove the duplicate entries from
// collection using LinkedHashSet
  
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
  
class GFG {
    public static void main(String[] args)
    {
        // making the collection object
        Collection<String> collection = new ArrayList<>();
  
        // adding the elements to the collection
        collection.add("Geeks");
        collection.add("For");
        collection.add("Geeks");
        collection.add("Internship");
        collection.add("Internship");
        collection.add("2021");
        collection.add("2021");
  
        // Displaying the collection elements
        System.out.println(
            "Displaying the initial collection\n");
        System.out.println(collection);
  
        // LinkedHashSet for deleting duplicate entries
        // in the collection by passing collection
        // in the constructor of the HashSet
        LinkedHashSet<String> hashSet
            = new LinkedHashSet<>(collection);
  
        // Displaying the HashSet
        System.out.println("\nDisplaying the HashSet\n");
        System.out.println(hashSet);
  
        // clearing all the elements of the collection
        collection.clear();
  
        // adding all the elements back
        // to the collection from HashSet
        collection.addAll(hashSet);
  
        // Displaying the collection
        System.out.println(
            "\nDisplaying the collection after deleting duplicates entries\n");
        System.out.println(collection);
    }
}


Output:

Displaying the initial collection

[Geeks, For, Geeks, Internship, Internship, 2021, 2021]

Displaying the HashSet

[Geeks, For, Internship, 2021]

Displaying the collection after deleting duplicates entries

[Geeks, For, Internship, 2021]



Last Updated : 03 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads