Java⾼级核⼼之玩转 JDK8 Collection和Lambda实战——总结篇

DBC 718 0
小例子
  //总价 35
            List<VideoOrder> videoOrders1 = Arrays.asList(
                    new VideoOrder("20190242812", "springboot教程", 3),
                    new VideoOrder("20194350812", "微服务SpringCloud", 5),
                    new VideoOrder("20190814232", "Redis教程", 9),
                    new VideoOrder("20190523812", "网页开发教程", 9),
                    new VideoOrder("201932324", "百万并发实战Netty", 9));

            //总价 54
            List<VideoOrder> videoOrders2 = Arrays.asList(
                    new VideoOrder("2019024285312", "springboot教程", 3),
                    new VideoOrder("2019081453232", "Redis教程", 9),
                    new VideoOrder("20190522338312", "网页开发教程", 9),
                    new VideoOrder("2019435230812", "Jmeter压力测试", 5),
                    new VideoOrder("2019323542411", "Git+Jenkins持续集成", 7),
                    new VideoOrder("2019323542424", "Idea全套教程", 21));


            //交集
            List<VideoOrder> intersectionList = videoOrders1.stream().filter(videoOrders2::contains).collect(Collectors.toList());

            System.out.println(intersectionList);


            //差集1
            List<VideoOrder> diffList1 =  videoOrders1.stream().filter(obj->!videoOrders2.contains(obj)).collect(Collectors.toList());

            System.out.println("差集1="+diffList1);

            List<VideoOrder> diffList2 =  videoOrders2.stream().filter(obj->!videoOrders1.contains(obj)).collect(Collectors.toList());
            System.out.println("差集2="+diffList2);



            //并集
            List<VideoOrder> allVideoOrder = videoOrders1.parallelStream().collect(Collectors.toList());
            allVideoOrder.addAll(videoOrders2);
            System.out.println("并集="+allVideoOrder);


            List<VideoOrder> allVideoOrderDistinct = allVideoOrder.parallelStream().distinct().collect(Collectors.toList());
            System.out.println("去重并集="+allVideoOrderDistinct);
小例子
        //两个订单平均价格
        double videoOrderAvg1 =  videoOrders1.stream().collect(Collectors.averagingInt(VideoOrder::getMoney)).doubleValue();

        System.out.println("订单列表1平均价格="+videoOrderAvg1);

        double videoOrderAvg2 =  videoOrders2.stream().collect(Collectors.averagingInt(VideoOrder::getMoney)).doubleValue();
        System.out.println("订单列表2平均价格="+videoOrderAvg2);


        //订单总价

        int totalMoney1 =  videoOrders1.stream().collect(Collectors.summingInt(VideoOrder::getMoney)).intValue();
        System.out.println("订单列表1总价="+totalMoney1);

        int totalMoney2 =  videoOrders2.stream().collect(Collectors.summingInt(VideoOrder::getMoney)).intValue();
        System.out.println("订单列表2总价="+totalMoney2);

发表评论 取消回复
表情 图片 链接 代码

分享