Java Stream - 从 CSV 检索重复记录 [英] Java Stream - Retrieving repeated records from CSV

查看:51
本文介绍了Java Stream - 从 CSV 检索重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了该网站并没有找到类似的内容.我是使用 Java 流的新手,但我知道它是循环命令的替代品.但是,我想知道是否有一种方法可以使用流过滤CSV文件,如下所示,结果中仅包含重复记录并按中心字段分组.

I searched the site and didn't find something similar. I'm newbie to using the Java stream, but I understand that it's a replacement for a loop command. However, I would like to know if there is a way to filter a CSV file using stream, as shown below, where only the repeated records are included in the result and grouped by the Center field.

初始 CSV 文件

最终结果

另外,同一对不能反向出现在最终结果中,如下表所示:

In addition, the same pair cannot appear in the final result inversely, as shown in the table below:

这不应该发生

有没有办法同时使用流和分组来做到这一点,因为理论上,执行任务需要两个循环?

Is there a way to do it using stream and grouping at the same time, since theoretically, two loops would be needed to perform the task?

提前致谢.

推荐答案

我从您的示例中了解到,如果除 ID 之外的所有属性都具有相同的值,您会将条目视为重复条目.您可以为此使用 anymatch:

What I understood from your examples is you consider an entry as duplicate if all the attributes have same value except the ID. You can use anymatch for this:

list.stream().filter(x ->
                list.stream().anyMatch(y -> isDuplicate(x, y))).collect(Collectors.toList())

那么 isDuplicate(x,y) 有什么作用?

这将返回一个 boolean.您可以在此方法中检查除 id 之外的所有条目是否具有相同的值:

This returns a boolean. You can check whether all the entries have same value except the id in this method:

private boolean isDuplicate(CsvEntry x, CsvEntry y) {
    return !x.getId().equals(y.getId())
            && x.getName().equals(y.getName())
            && x.getMother().equals(y.getMother())
            && x.getBirth().equals(y.getBirth());
}

我假设您已将所有条目视为 String.根据类型更改支票.这将为您提供具有相应 ID

I've assumed you've taken all the entries as String. Change the checks according to the type. This will give you the duplicate entries with their corresponding ID

这篇关于Java Stream - 从 CSV 检索重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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