用Stream流遍历两个集合,对比出数据差异。

今天开发一个需求时,要对A和B两个List集合遍历,并比较出集合A有,而集合B没有的值。
比如说List集合A有’one’,‘two’,‘three’三个值,List集合B有’one’,‘three’两个值。
那么最终打印’two’。
下面我就用stream流来做个例子。
例子中,集合A的泛型为HashMap,集合B的泛型为String。

Stream流中,forEach()方法用于一个流进行结尾动作,同时对最终过滤后的元素结果进行一个遍历操作。
我们可以在forEach()中直接写入类和其对应方法名,来对过滤后的元素进行操作。如以下的示例中的forEach(System.out::println)

    @org.junit.Test
    public void streamList(){



        HashMap<String,String> hashMapOne = new HashMap<>();
        hashMapOne.put("userid","one" );
        HashMap<String,String> hashMapTwo = new HashMap<>();
        hashMapTwo.put("userid","two" );
        HashMap<String,String> hashMapThree = new HashMap<>();
        hashMapThree.put("userid","three" );
        List<HashMap<String,String>> listMap = new ArrayList<>();
        listMap.add(hashMapOne);
        listMap.add(hashMapTwo);
        listMap.add(hashMapThree);


        List<String> elementList = new ArrayList<>();
        elementList.add("one");
        //elementList.add("two");
        elementList.add("three");

        
        //通过filter方法,过滤每个元素。你需要在filter方法内通过lambda表达式,来定义对每个元素的过滤流程。
        listMap.stream().filter( itemmap -> {

            int hit = 0;//是否命中; 0:未命中  >0:命中
            for (int i = 0; i < elementList.size(); i++) {
                if (elementList.get(i).equals(itemmap.get("userid"))){
                    hit++;
                }

            };
            return hit == 0 ? true:false;//如果返回值为true,则将该元素交给下一个Stream流环节,也就是交给forEach方法来处理。
        }).forEach(System.out::println);//通过filter方法过滤后的元素,再通过forEach方法对每个元素进行使用。
                                        // 我们可以在forEach方法内,使用System.out.println来对过滤后的元素打印。


    }

在这里插入图片描述

同样的,forEach()方法也可以如同filter()方法那样,在方法里头通过lambda表达式,来对元素进行更为细致的操作。如以下示例中的

forEach( filterResultItem -> {
filterResult.add(filterResultItem.get(“userid”));
});

    @org.junit.Test
    public void streamList(){



        HashMap<String,String> hashMapOne = new HashMap<>();
        hashMapOne.put("userid","one" );
        HashMap<String,String> hashMapTwo = new HashMap<>();
        hashMapTwo.put("userid","two" );
        HashMap<String,String> hashMapThree = new HashMap<>();
        hashMapThree.put("userid","three" );
        List<HashMap<String,String>> listMap = new ArrayList<>();
        listMap.add(hashMapOne);
        listMap.add(hashMapTwo);
        listMap.add(hashMapThree);


        List<String> elementList = new ArrayList<>();
        elementList.add("one");
        //elementList.add("two");
        elementList.add("three");

        ArrayList<String> filterResult = new ArrayList<>();

        //通过filter方法,过滤每个元素。你需要在filter方法内通过lambda表达式,来定义对每个元素的过滤流程。
        listMap.stream().filter( itemmap -> {

            int hit = 0;//是否命中; 0:未命中  >0:命中
            for (int i = 0; i < elementList.size(); i++) {
                if (elementList.get(i).equals(itemmap.get("userid"))){
                    hit++;
                }

            };
            return hit == 0 ? true:false;//如果返回值为true,则将该元素交给下一个Stream流环节,也就是交给forEach方法来处理。
        }).forEach( filterResultItem -> {
            filterResult.add(filterResultItem.get("userid"));
        });//通过filter方法过滤后的元素,再通过forEach方法对每个元素进行使用。
           // 不仅仅是类似于System.out::println这样的形式来指定类和其对应的方法名对过滤后的元素进行操作。
            //  我们也可以在forEach方法内,以lambda表达式的方式,来对过滤后的元素进行更为细致的操作。
        System.out.println(filterResult);
        
    }

在这里插入图片描述

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值