Java 8,Streams查找重复元素 [英] Java 8, Streams to find the duplicate elements

查看:105
本文介绍了Java 8,Streams查找重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图列出整数列表中的重复元素,例如,

I am trying to list out duplicate elements in the integer list say for eg,

List<Integer> numbers = Arrays.asList(new Integer[]{1,2,1,3,4,4});    

使用jdk的Streams 8.是否有人试用过。要删除重复项,我们可以使用distinct()api。但是如何找到重复的元素呢?有人可以帮帮我吗?

using Streams of jdk 8. Has anybody tried out. To remove the duplicates we can use the distinct() api. But what about finding the duplicated elements ? Anybody can help me out ?

推荐答案

你可以用Collections.frequency

numbers.stream().filter(i -> Collections.frequency(numbers, i) >1)
                .collect(Collectors.toSet()).forEach(System.out::println);

这篇关于Java 8,Streams查找重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆