Java 如何查找流中的重复元素

Java 如何查找流中的重复元素

在Java面试和许多学生的考试中,找到流中的重复元素是一个常见的问题。Java提供了几种查找重复元素的方法,我们主要关注两种方法:第一种是使用Java Collection Framework的Set,另一种是使用流的内置方法Collections.frequency()。

Java程序查找流中的重复元素

在讨论从数据集合中获取重复项的不同方法之前,有必要首先讨论一下filter()方法。它将是示例程序的关键部分。

filter()

它允许我们根据指定的条件筛选流的元素。它是用于在流项目上应用特定行为的高阶函数的一部分。此方法接受一个谓词作为参数,并返回与谓词匹配的元素列表。

语法

filter(predicate);

使用Java集合框架中的Set

Set是Java集合接口的子接口,不允许重复值。它与数学集合非常相似。我们可以使用add()方法,只将不相似的元素追加到集合中。要使用Set接口的属性,我们需要使用实现了这个接口的HashSet类。

示例

以下示例说明了在流中使用Set接口查找重复元素。

方法

  • 使用Arrays.asList()方法创建一个列表,以存储固定大小的列表。

  • 然后,使用HashSet类定义一个Set,以仅存储不相似的元素。

  • 现在,使用filter()方法与stream()和forEach()过滤出仅重复项。这里,stream()指定以流形式输入,我们将使用forEach()迭代和打印重复元素。

import java.util.*;
public class Duplicate {
   public static void main(String []args) {
      // create a list with duplicate items
      List<Integer> itemsList = Arrays.asList(10, 12, 10, 33, 40, 40, 61, 61);
      // declaring a new Set 
      Set<Integer> newitemSet = new HashSet<>();
      System.out.println("The list of duplicate Items: ");
      itemsList.stream() // converting list to stream
         .filter(nums -> !newitemSet.add(nums)) // to filter out the elementsthat are not added to the set
         .forEach(System.out::println); // print the duplicates
   }
}

输出

The list of duplicate Items: 
10
40
61

使用Collections.frequency()方法

从流或集合中过滤重复元素的另一种简单方法是使用’java.util’包中的Collections.frequency()方法,该方法用于返回指定集合中元素的总数。

语法

Collections.frequency(nameOfCollection, obj);

这里,

nameOfCollection 表示流,obj表示需要确定频率的元素。

示例

在以下示例中,我们将使用Collections.frequency()方法计算流中每个元素的出现次数,然后返回出现超过一次的元素。我们将打印出重复元素的全部出现次数列表以及计数。

import java.util.*;
public class FindDuplicates {
   public static void main(String[] args) {
      // create a list with duplicate items
      List<Integer> itemslist = Arrays.asList(10, 12, 10, 10, 33, 40, 40, 61, 61);
      System.out.println("The list of duplicate Items with frequency: ");
      itemslist.stream() // converting list to stream 
         .filter(itr -> Collections.frequency(itemslist, itr) > 1) // checking the frequency of duplicate items
         .forEach(System.out::println); // printing the frequency of duplicate items
      System.out.println("Count of duplicate items: ");    
      // to count the duplicate items    
      System.out.println(itemslist.stream()
       .filter(itr -> Collections.frequency(itemslist, itr) > 1)
       .count());
    }
}

输出

The list of duplicate Items with frequency: 
10
10
10
40
40
61
61
Count of duplicate items: 
7

示例

这里有另一个示例,我们将同时使用Set接口和Collections.frequency()方法来获取重复的元素。Collections.frequency()方法将计算流中每个元素的出现次数,然后将计数大于1的元素收集到一个Set中以去除重复项。最终的Set将只包含流中的重复元素。

import java.util.stream.*;
import java.util.*;
public class FindDuplicates {
   public static void main(String[] args) {
      // create a list with duplicate items
      List<Integer> itemslist = Arrays.asList(10, 12, 10, 10, 33,40, 40, 61, 61);
      // set to store duplicate items
      Set<Integer> duplicates = itemslist.stream()
         .filter(itr -> Collections.frequency(itemslist, itr) > 1) // checking the frequency of duplicate items
         .collect(Collectors.toSet()); // adding only duplicate items to set
      // printing the duplicate items    
      System.out.println("The list of duplicate Items:" + duplicates); 
   }
}

输出

The list of duplicate Items:[40, 10, 61]

结论

在本节中,我们将通过上述示例和概念的一些关键点来总结我们的讨论。我们可以使用filter()方法从数据集合中过滤出特定类型的元素。它在后台运行,通过将Predicate应用于每个元素来工作。Set接口具有仅存储不同元素的能力,这使其成为完成给定任务的一个极好选择。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程