繁体   English   中英

解决迭代的更好方法

[英]Better approach to solve the iteration

List<emp1> empList=new ArrayList<>();
        empList.add(new emp1(1,"code1","a1"));
        empList.add(new emp1(2,"code2","a2"));
Set<emp1> empSet=new HashSet<>();
        empSet.add(new emp1(1,"code3","a3"));

for(emp1 e:empList) {
            for(emp1 a:empSet) {
                if(e.getB().equals(a.getB())||e.getA()==a.getA()||e.getC().equals(a.getC())) {
                    System.out.println("equals");
                }
            }
        }

class emp1{
    int a;
    String b;
    String c;

如何使用 lambda 或任何其他更好的方法来实现这一点? 它应该比较列表中的每个元素来设置并判断它是否存在? 需要比较每个元素 a,b,c ,如果存在它应该返回等于

尽管他 forEach 并没有多大意义,但您可以将其实现为:

    empList.stream()
            .filter(empSet::contains)
            .map(a -> "equals")
            .forEach(System.out::println);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM