Lambda表达式详解(二)

概要

本篇将通过几个案例来说明Lambda和Stream的联合使用,通过本篇你将发现使用Lambda能在一定的场景下大大的简化代码。

案例

Filter的使用

测试案例

    @Before
    public void init() {
        log.info("begin to init data!");
        ruleList = new ArrayList<>();
        ruleList.add(new GoodsRule(1L, 1, 1, "3"));
        ruleList.add(new GoodsRule(1L, 2, 1, "呵呵"));
        ruleList.add(new GoodsRule(1L, 2, 2, "3"));
        ruleList.add(new GoodsRule(1L, 3, 1, "1232"));
        ruleList.add(new GoodsRule(2L, 1, 1, "3"));
        ruleList.add(new GoodsRule(2L, 2, 1, "qqq"));
        log.info("end to init data!");
    }

    @Test
    public void testStreamFilter() {
        log.info("过滤前:");
        ruleList.forEach(rule -> log.info(gs.toJson(rule)));
        log.info("过滤后:");
        ruleList.stream().filter(rule -> Long.valueOf(2).equals(rule.goodsId))
                        .forEach(rule -> log.info(gs.toJson(rule)));

    }

执行结果

INFO  [VIPApp] 2018-08-11 17:16:30.939 [main] com.zyxr.vip.test.mall.AdvisorTest ? - begin to init data!
INFO  [VIPApp] 2018-08-11 17:16:30.940 [main] com.zyxr.vip.test.mall.AdvisorTest 260 - end to init data!
INFO  [VIPApp] 2018-08-11 17:16:30.940 [main] com.zyxr.vip.test.mall.AdvisorTest 265 - 过滤前:
INFO  [VIPApp] 2018-08-11 17:16:30.940 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:16:30.941 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:16:30.941 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:16:30.941 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO  [VIPApp] 2018-08-11 17:16:30.941 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:16:30.941 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:16:30.941 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - 过滤后:
INFO  [VIPApp] 2018-08-11 17:16:30.945 [main] com.zyxr.vip.test.mall.AdvisorTest 269 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:16:30.945 [main] com.zyxr.vip.test.mall.AdvisorTest 269 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}

自定义Filter

测试案例

@Test
    public void testStreamFilter1() {
        //goodsId为2的过滤器
        Predicate goodsRulePredicate = rule -> Long.valueOf(2).equals(rule.goodsId);
        //firstType为2的过滤器
        Predicate fistTypePredicate = rule -> Integer.valueOf(2).equals(rule.firstType);
        log.info("过滤前:");
        ruleList.forEach(rule -> log.info(gs.toJson(rule)));
        //过滤firstType为2的GoodsRule
        log.info("过滤后:");
        ruleList.stream().filter(fistTypePredicate).forEach(rule -> log.info(gs.toJson(rule)));

        log.info("过滤前:");
        ruleList.forEach(rule -> log.info(gs.toJson(rule)));
        //过滤goodsId为2和firstType为2的GoodsRule
        log.info("过滤后:");
        ruleList.stream().filter(goodsRulePredicate).filter(fistTypePredicate)
                        .forEach(rule -> log.info(gs.toJson(rule)));

    }

执行结果

INFO  [VIPApp] 2018-08-11 17:19:43.758 [main] com.zyxr.vip.test.mall.AdvisorTest 252 - begin to init data!
INFO  [VIPApp] 2018-08-11 17:19:43.758 [main] com.zyxr.vip.test.mall.AdvisorTest 260 - end to init data!
INFO  [VIPApp] 2018-08-11 17:19:43.759 [main] com.zyxr.vip.test.mall.AdvisorTest 269 - 过滤前:
INFO  [VIPApp] 2018-08-11 17:19:43.761 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:19:43.761 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:19:43.761 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:19:43.761 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO  [VIPApp] 2018-08-11 17:19:43.761 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:19:43.761 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:19:43.761 [main] com.zyxr.vip.test.mall.AdvisorTest 272 - 过滤后:
INFO  [VIPApp] 2018-08-11 17:19:43.764 [main] com.zyxr.vip.test.mall.AdvisorTest 273 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:19:43.764 [main] com.zyxr.vip.test.mall.AdvisorTest 273 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:19:43.764 [main] com.zyxr.vip.test.mall.AdvisorTest 273 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:19:43.764 [main] com.zyxr.vip.test.mall.AdvisorTest 275 - 过滤前:
INFO  [VIPApp] 2018-08-11 17:19:43.764 [main] com.zyxr.vip.test.mall.AdvisorTest 276 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:19:43.764 [main] com.zyxr.vip.test.mall.AdvisorTest 276 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:19:43.764 [main] com.zyxr.vip.test.mall.AdvisorTest 276 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:19:43.765 [main] com.zyxr.vip.test.mall.AdvisorTest 276 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO  [VIPApp] 2018-08-11 17:19:43.765 [main] com.zyxr.vip.test.mall.AdvisorTest 276 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:19:43.765 [main] com.zyxr.vip.test.mall.AdvisorTest 276 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:19:43.765 [main] com.zyxr.vip.test.mall.AdvisorTest 278 - 过滤后:
INFO  [VIPApp] 2018-08-11 17:19:43.765 [main] com.zyxr.vip.test.mall.AdvisorTest ? - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}

limit使用

测试案例

 @Before
    public void init() {
        log.info("begin to init data!");
        ruleList = new ArrayList<>();
        ruleList.add(new GoodsRule(1L, 1, 1, "3"));
        ruleList.add(new GoodsRule(1L, 2, 1, "呵呵"));
        ruleList.add(new GoodsRule(1L, 2, 2, "3"));
        ruleList.add(new GoodsRule(1L, 3, 1, "1232"));
        ruleList.add(new GoodsRule(2L, 1, 1, "3"));
        ruleList.add(new GoodsRule(2L, 2, 1, "qqq"));
        log.info("end to init data!");
    }

    @Test
    public void testStreamLimit() {
        log.info("限制前:");
        ruleList.forEach(rule -> log.info(gs.toJson(rule)));
        log.info("限制后:");
        ruleList.stream().limit(3).forEach(rule -> log.info(gs.toJson(rule)));
    }

执行结果

INFO  [VIPApp] 2018-08-11 17:22:47.743 [main] com.zyxr.vip.test.mall.AdvisorTest 252 - begin to init data!
INFO  [VIPApp] 2018-08-11 17:22:47.743 [main] com.zyxr.vip.test.mall.AdvisorTest 260 - end to init data!
INFO  [VIPApp] 2018-08-11 17:22:47.743 [main] com.zyxr.vip.test.mall.AdvisorTest 265 - 限制前:
INFO  [VIPApp] 2018-08-11 17:22:47.745 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:22:47.745 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:22:47.745 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:22:47.745 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO  [VIPApp] 2018-08-11 17:22:47.746 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:22:47.746 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:22:47.746 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - 限制后:
INFO  [VIPApp] 2018-08-11 17:22:47.750 [main] com.zyxr.vip.test.mall.AdvisorTest 268 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:22:47.750 [main] com.zyxr.vip.test.mall.AdvisorTest 268 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:22:47.750 [main] com.zyxr.vip.test.mall.AdvisorTest 268 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}

sort使用

测试案例

    @Before
    public void init() {
        log.info("begin to init data!");
        ruleList = new ArrayList<>();
        ruleList.add(new GoodsRule(1L, 1, 1, "3"));
        ruleList.add(new GoodsRule(1L, 2, 1, "呵呵"));
        ruleList.add(new GoodsRule(1L, 2, 2, "3"));
        ruleList.add(new GoodsRule(1L, 3, 1, "1232"));
        ruleList.add(new GoodsRule(2L, 1, 1, "3"));
        ruleList.add(new GoodsRule(2L, 2, 1, "qqq"));
        log.info("end to init data!");
    }

    @Test
    public void testStreamSort() {
        log.info("排序前:");
        ruleList.forEach(rule -> log.info(gs.toJson(rule)));
        //按照goodsId降序然后取前3条记录
        log.info("排序后:");
        ruleList.stream().sorted((GoodsRule o1, GoodsRule o2) -> (int) (o2.goodsId - o1.goodsId))
                        .limit(3).forEach(rule -> log.info(gs.toJson(rule)));
    }

执行结果

INFO  [VIPApp] 2018-08-11 17:24:56.316 [main] com.zyxr.vip.test.mall.AdvisorTest 252 - begin to init data!
INFO  [VIPApp] 2018-08-11 17:24:56.316 [main] com.zyxr.vip.test.mall.AdvisorTest 260 - end to init data!
INFO  [VIPApp] 2018-08-11 17:24:56.316 [main] com.zyxr.vip.test.mall.AdvisorTest 265 - 排序前:
INFO  [VIPApp] 2018-08-11 17:24:56.317 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:24:56.317 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:24:56.317 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:24:56.317 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO  [VIPApp] 2018-08-11 17:24:56.317 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:24:56.318 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:24:56.318 [main] com.zyxr.vip.test.mall.AdvisorTest 268 - 排序后:
INFO  [VIPApp] 2018-08-11 17:24:56.321 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:24:56.321 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:24:56.321 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}

min和max使用

测试案例


    @Before
    public void init() {
        log.info("begin to init data!");
        ruleList = new ArrayList<>();
        ruleList.add(new GoodsRule(1L, 1, 1, "3"));
        ruleList.add(new GoodsRule(1L, 2, 1, "呵呵"));
        ruleList.add(new GoodsRule(1L, 2, 2, "3"));
        ruleList.add(new GoodsRule(1L, 3, 1, "1232"));
        ruleList.add(new GoodsRule(2L, 1, 1, "3"));
        ruleList.add(new GoodsRule(2L, 2, 1, "qqq"));
        log.info("end to init data!");
    }


    @Test
    public void testStreamMinAndMax() {
        ruleList.forEach(rule -> log.info(gs.toJson(rule)));
        //按照goodsId降序然后取前3条记录
        GoodsRule minRule = ruleList.stream().min((GoodsRule o1, GoodsRule o2) -> (int) (o2.goodsId - o1.goodsId)).get();
        log.info("minRule is :{}",gs.toJson(minRule));
        GoodsRule maxRule = ruleList.stream().max((GoodsRule o1, GoodsRule o2) -> (int) (o2.goodsId - o1.goodsId)).get();
        log.info("maxRule is :{}",gs.toJson(maxRule));
    }

执行结果

INFO  [VIPApp] 2018-08-11 17:26:59.922 [main] com.zyxr.vip.test.mall.AdvisorTest 252 - begin to init data!
INFO  [VIPApp] 2018-08-11 17:26:59.922 [main] com.zyxr.vip.test.mall.AdvisorTest ? - end to init data!
INFO  [VIPApp] 2018-08-11 17:26:59.923 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:26:59.923 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO  [VIPApp] 2018-08-11 17:26:59.923 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:26:59.923 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO  [VIPApp] 2018-08-11 17:26:59.923 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:26:59.924 [main] com.zyxr.vip.test.mall.AdvisorTest 266 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO  [VIPApp] 2018-08-11 17:26:59.928 [main] com.zyxr.vip.test.mall.AdvisorTest 269 - minRule is :{"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO  [VIPApp] 2018-08-11 17:26:59.930 [main] com.zyxr.vip.test.mall.AdvisorTest 271 - maxRule is :{"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}

并行的stream

测试案例

    @Before
    public void init() {
        log.info("begin to init data!");
        ruleList = new ArrayList<>();
        ruleList.add(new GoodsRule(1L, 1, 1, "3"));
        ruleList.add(new GoodsRule(1L, 2, 1, "呵呵"));
        ruleList.add(new GoodsRule(1L, 2, 2, "3"));
        ruleList.add(new GoodsRule(1L, 3, 1, "1232"));
        ruleList.add(new GoodsRule(2L, 1, 1, "3"));
        ruleList.add(new GoodsRule(2L, 2, 1, "qqq"));
        log.info("end to init data!");
    }


    @Test
    public void testParallelStream() {
        LongSummaryStatistics longSummaryStatistics = ruleList.parallelStream().mapToLong(x -> x.goodsId)
                        .summaryStatistics();
        log.info("ruleList中最大的goodsId:{}",longSummaryStatistics.getMax());
        log.info("ruleList中最小的goodsId:{}",longSummaryStatistics.getMin());
        log.info("ruleList中goodsId总和为:{}",longSummaryStatistics.getSum());
        log.info("ruleList中记录数为:{}",longSummaryStatistics.getCount());
        log.info("ruleList中goodsId的平均值为:{}",longSummaryStatistics.getAverage());

    }

执行结果

INFO  [VIPApp] 2018-08-11 17:28:51.940 [main] com.zyxr.vip.test.mall.AdvisorTest ? - begin to init data!
INFO  [VIPApp] 2018-08-11 17:28:51.941 [main] com.zyxr.vip.test.mall.AdvisorTest 260 - end to init data!
INFO  [VIPApp] 2018-08-11 17:28:51.953 [main] com.zyxr.vip.test.mall.AdvisorTest ? - ruleList中最大的goodsId:2
INFO  [VIPApp] 2018-08-11 17:28:51.953 [main] com.zyxr.vip.test.mall.AdvisorTest 269 - ruleList中最小的goodsId:1
INFO  [VIPApp] 2018-08-11 17:28:51.953 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - ruleList中goodsId总和为:8
INFO  [VIPApp] 2018-08-11 17:28:51.953 [main] com.zyxr.vip.test.mall.AdvisorTest 271 - ruleList中记录数为:6
INFO  [VIPApp] 2018-08-11 17:28:51.953 [main] com.zyxr.vip.test.mall.AdvisorTest 272 - ruleList中goodsId的平均值为:1.3333333333333333

总结

通过Lambda表达式和集合Stream的使用案例来看,在集合操作场景,也许能给广大程序员提供一种新的解决思路。

你可能感兴趣的:(java,lambda,stream,jdk1.8新特性,集合)