Java程序 从集合中删除所有重复的条目
我们知道HashSet只包含唯一的元素,即不允许有重复的条目,由于我们的目的是将重复的条目从集合中移除,所以为了从集合中移除所有重复的条目,我们将使用HashSet。HashSet类实现了Set接口,由一个哈希表支持,而哈希表实际上是一个HashMap实例。该类还为基本操作提供了恒定的时间性能,如添加、删除、包含和大小,假定哈希函数在桶中正确地分散了元素。HashSet通常被用来检查一个元素是否存在于一个列表中。
注意: 因为我们使用的是HashSet,所以插入的顺序不会被保留,每次我们运行代码的时候都会得到一些不同的输出(元素的顺序会不同)。因此,如果我们想在插入时保留元素的顺序,那么我们应该使用LinkedHashSet。
基本上有两种方法来删除集合中的重复条目。
1.使用哈希集
2.使用LinkHashSet
现在让我们来看看使用java程序的实现,通过使用这两种方法逐一删除重复的条目:-
1.使用HashSet
// 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);
}
}
输出:
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.使用LinkedHashSet
// 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);
}
}
输出:
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]