📜  从集合中删除所有重复条目的Java程序

📅  最后修改于: 2022-05-13 01:54:20.671000             🧑  作者: Mango

从集合中删除所有重复条目的Java程序

我们知道 HashSet 只包含唯一元素,即不允许重复条目,并且由于我们的目标是从集合中删除重复条目,因此为了从集合中删除所有重复条目,我们将使用 HashSet HashSet 类实现了 Set 接口,由一个哈希表支持,它实际上是一个 HashMap 实例。该类还为基本操作(如添加、删除、包含和大小)提供恒定的时间性能,假设散列函数在存储桶中正确分散元素。 HashSet 通常用于检查元素是否存在于列表中。

注意:由于我们使用的是 HashSet,因此不会保留插入顺序,每次运行代码时,我们都会得到一些不同的输出(元素的顺序会有所不同)。所以如果我们想在插入时保留元素的顺序,那么我们应该使用LinkedHashSet。

基本上有 从集合中删除重复条目的两种方法:

  1. 使用哈希集
  2. 使用 LinkHashSet

现在让我们看看使用Java程序通过两种方法一一去除重复条目的实现:-

1. 使用哈希集

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 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 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);
    }
}


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 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 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);
    }
}


输出:



2. 使用 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 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 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);
    }
}

输出: