码迷,mamicode.com
首页 > 编程语言 > 详细

高效 告别996,开启java高效编程之门 3-23汇总操作实战案例

时间:2020-05-10 10:24:59      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:version   bsp   plugins   common   package   process   span   intellij   jdk   

1    重点:

1.1  reduce demo组成

1.2  准备数据

1.3  并行计算

 

2    规约操作实战案例

测试类:

package com.imooc.zhangxiaoxi.stream;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.ArrayList;

/**
 * ReduceAndCollect
 *  归约与汇总操作
 * @author 魏豆豆
 * @date 2020/5/7
 */
public class ReduceAndCollect {

    @Test
    /**
     * 归约reduce Demo
     * 需求:从一批订单中找到数量和总额,计算平均商品价格
     */
    public void reduceTest(){

        /**
         * 订单对象
         * @Data 注入SetGet方法                 后边lombok会讲
         * @AllArgsConstructor  注入传参方法
         */
        class Order{
            private Integer id;             //订单编号
            private Integer orderCount;     //订单数量
            private Double orderTotalPrice; //订单总价

            public Order(Integer id, Integer orderCount, Double orderTotalPrice) {
                this.id = id;
                this.orderCount = orderCount;
                this.orderTotalPrice = orderTotalPrice;
            }

            public Integer getId() {
                return id;
            }

            public void setId(Integer id) {
                this.id = id;
            }

            public Integer getOrderCount() {
                return orderCount;
            }

            public void setOrderCount(Integer orderCount) {
                this.orderCount = orderCount;
            }

            public Double getOrderTotalPrice() {
                return orderTotalPrice;
            }

            public void setOrderTotalPrice(Double orderTotalPrice) {
                this.orderTotalPrice = orderTotalPrice;
            }
        }

        //准备数据
        ArrayList<Order> list = Lists.newArrayList();
        list.add(new Order(1,3,9.00));
        list.add(new Order(1,2,100.00));
        list.add(new Order(1,1,11.00));


        /**
         * reduce (初始逻辑,计算规则,并行计算规则)
         * 需求:计算商品平均单价
         */
        Order order = list.stream()
                //并行计算
                //.parallel()
                .reduce(
                        //1     初始逻辑
                        new Order(0,0,0.00),
                        //2     计算规则
                        (Order order1,Order order2)->{
                            System.out.println("计算逻辑!!!");
                            Integer orderCount = order1.getOrderCount()+order2.getOrderCount();
                            Double orderPrice = order1.getOrderTotalPrice()+order2.getOrderTotalPrice();
                            return new Order(0,orderCount,orderPrice);
                        },
                        //3     并行计算
                        (Order order1,Order order2)->{
                            System.out.println("并行计算!!!");
                            Integer orderCount = order1.getOrderCount()+order2.getOrderCount();
                            Double orderPrice = order1.getOrderTotalPrice()+order2.getOrderTotalPrice();
                            return new Order(0,orderCount,orderPrice);
                        });
        System.out.println(JSONObject.toJSONString(order,true));
    }
}

 

 

 

打印日志:

D:\java\jdk\jdk9\jdk-9+181_windows-x64_ri\java-se-9-ri\jdk-9\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=28296:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit-rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit5-rt.jar;F:\xiangmu3\Xin\test996\target\test-classes;F:\xiangmu3\Xin\test996\target\classes;F:\xiangmu3\Xin\test996\lib\com\google\guava\guava\28.2-jre\guava-28.2-jre.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;F:\xiangmu3\Xin\test996\lib\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;F:\xiangmu3\Xin\test996\lib\org\checkerframework\checker-qual\2.10.0\checker-qual-2.10.0.jar;F:\xiangmu3\Xin\test996\lib\com\google\errorprone\error_prone_annotations\2.3.4\error_prone_annotations-2.3.4.jar;F:\xiangmu3\Xin\test996\lib\com\google\j2objc\j2objc-annotations\1.3\j2objc-annotations-1.3.jar;F:\xiangmu3\Xin\test996\lib\junit\junit\4.12\junit-4.12.jar;F:\xiangmu3\Xin\test996\lib\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\xiangmu3\Xin\test996\lib\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.zhangxiaoxi.stream.ReduceAndCollect,reduceTest
计算逻辑!!!
计算逻辑!!!
计算逻辑!!!
{
    "id":0,
    "orderCount":6,
    "orderTotalPrice":120.0
}

Process finished with exit code 0

 

高效 告别996,开启java高效编程之门 3-23汇总操作实战案例

标签:version   bsp   plugins   common   package   process   span   intellij   jdk   

原文地址:https://www.cnblogs.com/1446358788-qq/p/12861874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!