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

Posted 1446358788-qq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高效 告别996,开启java高效编程之门 3-23汇总操作实战案例相关的知识,希望对你有一定的参考价值。

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:javajdkjdk9jdk-9+181_windows-x64_rijava-se-9-rijdk-9injava.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4libidea_rt.jar=28296:D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4in" -Dfile.encoding=UTF-8 -classpath "D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4libidea_rt.jar;D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4pluginsjunitlibjunit-rt.jar;D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4pluginsjunitlibjunit5-rt.jar;F:xiangmu3Xin	est996	arget	est-classes;F:xiangmu3Xin	est996	argetclasses;F:xiangmu3Xin	est996libcomgoogleguavaguava28.2-jreguava-28.2-jre.jar;F:xiangmu3Xin	est996libcomgoogleguavafailureaccess1.0.1failureaccess-1.0.1.jar;F:xiangmu3Xin	est996libcomgoogleguavalistenablefuture9999.0-empty-to-avoid-conflict-with-guavalistenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;F:xiangmu3Xin	est996libcomgooglecodefindbugsjsr3053.0.2jsr305-3.0.2.jar;F:xiangmu3Xin	est996liborgcheckerframeworkchecker-qual2.10.0checker-qual-2.10.0.jar;F:xiangmu3Xin	est996libcomgoogleerrorproneerror_prone_annotations2.3.4error_prone_annotations-2.3.4.jar;F:xiangmu3Xin	est996libcomgooglej2objcj2objc-annotations1.3j2objc-annotations-1.3.jar;F:xiangmu3Xin	est996libjunitjunit4.12junit-4.12.jar;F:xiangmu3Xin	est996liborghamcresthamcrest-core1.3hamcrest-core-1.3.jar;F:xiangmu3Xin	est996libcomalibabafastjson1.2.58fastjson-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汇总操作实战案例的主要内容,如果未能解决你的问题,请参考以下文章

高效 告别996,开启java高效编程之门 4-6本节总结

高效 告别996,开启java高效编程之门 4-6本节总结

高效 告别996,开启java高效编程之门 3-26去重

高效 告别996,开启java高效编程之门 3-26去重

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

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