java 8中两个列表对象的交集 [英] Intersection of Two Lists Objects in java 8

查看:47
本文介绍了java 8中两个列表对象的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 8 中两个列表对象的交集.有人能告诉我我做错了什么吗?

An intersection of Two Lists Objects in java 8. Can some tell me what am I doing wrong?

List<Student> originalStudent = new ArrayList<>();
List<Student> newStudent = new ArrayList<>();

List<Student> intersectListStudent = new LinkedList<>()

originalStudent.add(new Student("William", "Tyndale",1));
originalStudent.add(new Student("Jonathan", "Edwards",2));
originalStudent.add(new Student("Martin", "Luther"),3);

newStudent.add(new Student("Jonathan", "Edwards",2));
newStudent.add(new Student("James", "Tyndale",4));
newStudent.add(new Student("Roger", "Moore",5));


originalStudent.forEach(n ->
        newStudent.stream()
                .filter(db -> !n.getName().equals(db.getName()) &&
                        !n.getLastName().equals(db.getLastName()))
                    .forEach(student-> intersectListStudent .add(student)));

推荐答案

有人能告诉我我做错了什么吗?

Can some tell me what am I doing wrong?

您违反了 简而言之,流不应该在通过管道执行操作时修改另一个集合.我还没有测试你的代码,但是,这不是你应该处理流的方式.

You violate the Side-effects principle of java-stream which in a nutshell says that a stream shouldn't modify another collection while performing the actions through the pipelines. I haven't tested your code, however, this is not a way you should treat streams.

如何做得更好?

只需使用 List::contains 在过滤器的谓词中删除唯一值.

Simply use the List::contains in the filter's predicate to get rid of the unique values.

List<Student> students = originalStudent.stream()
                                        .filter(newStudent::contains)
                                        .collect(Collectors.toList());

这个解决方案(理解方法List::contains) 基于使用 Object::equals.因此,需要在 Student 类中重写完全相同的方法.

This solution (understand the method List::contains) is based on the implemented equality comparison using Object::equals. Hence, there is needed to override the very same method in the class Student.

请注意,自动覆盖 Object::equals 会介意 id 进行相等计算.因此,平等将仅基于名称和姓氏.(感谢 @nullpointer).

Please, be aware that that automatically overriding the Object::equals will mind the id to the equality computation. Therefore the equality will be based on the name and surname only. (thanks to @nullpointer).

没有覆盖 Object::equals 吗?

Without the Object::equals overridden?

您必须使用另一个 stream 和返回 true<的方法 Stream::anyMatchfilter 中执行比较/code> 如果谓词是合格的.

You have to perform the comparison in the filter using another stream and the method Stream::anyMatch which returns true if the predicate is qualified.

List<Student> students = originalStudent.stream()
              .filter(os -> newStudent.stream()                    // filter
                  .anyMatch(ns ->                                  // compare both
                       os.getName().equals(ns.getName() &&         // name
                       os.getLastName().equals(ns.getLastName()))) // last name
              .collect(Collectors.toList());

这篇关于java 8中两个列表对象的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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