Java Program To Remove All The Duplicate Entries From The Collection
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 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:-
1. Using HashSet
Java
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
class GFG {
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add( "Geeks" );
collection.add( "For" );
collection.add( "Geeks" );
collection.add( "Internship" );
collection.add( "Internship" );
collection.add( "2021" );
collection.add( "2021" );
System.out.println(
"Displaying the initial collection\n" );
System.out.println(collection);
HashSet<String> hashSet = new HashSet<>(collection);
System.out.println( "\nDisplaying the HashSet\n" );
System.out.println(hashSet);
collection.clear();
collection.addAll(hashSet);
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
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
class GFG {
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add( "Geeks" );
collection.add( "For" );
collection.add( "Geeks" );
collection.add( "Internship" );
collection.add( "Internship" );
collection.add( "2021" );
collection.add( "2021" );
System.out.println(
"Displaying the initial collection\n" );
System.out.println(collection);
LinkedHashSet<String> hashSet
= new LinkedHashSet<>(collection);
System.out.println( "\nDisplaying the HashSet\n" );
System.out.println(hashSet);
collection.clear();
collection.addAll(hashSet);
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
Share your thoughts in the comments
Please Login to comment...