Java Program To Remove All The Duplicate Entries From The Collection - GeeksforGeeks

03 Mar, 2021
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:
Using HashSetUsing LinkHashSet
Using HashSet
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:-
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]
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]
Java-Collections
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character