1    重点:

1.1  collect demo

1.2  merge方法(见demo中并行计算逻辑)

1.3  并行计算方法

1.4  lombok的优缺点

http://blog.itpub.net/69908877/viewspace-2676272/

 

2    merge方法(结合demo)

merge是说如果从map1中发现key,那么就把value作为参数,map1中的value也作为参数 传递进来。然后你去定义这两个key重合的value如何处理。

如果key没在map1中出现,做的操作就是把value push到map1中。

 

 

3    汇总操作实战案例

测试类:

 /**
     *     汇总collect Demo
     *      需求:从一批订单中分别找到用户下的数量和总额,按照用户计算平均商品价格
     */
    @Test
    public void collectTest(){

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

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

            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;
            }

            public String getUserID() {
                return userID;
            }

            public void setUserID(String userID) {
                this.userID = userID;
            }
        }

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

        /**
         * 汇总操作
         * 三个参数,第一个此初始化,第二个计算逻辑,第三个并行计算逻辑
         */
        Map<String,Order> colletMap =list.stream()
                //.parallel()
                .collect(
                        //第一个参数,初始化一个容器
                        () -> {
                            System.out.println("初始化逻辑");
                            return new HashMap<String, Order>();
                        },
                        //第二个传参数,实现计算逻辑,如果map中有该订单,则在该map中分别进行订单金额,订单数量的累加
                        (HashMap<String, Order> map, Order newOrder) -> {
                            System.out.println("计算逻辑");
                            String sUserID = newOrder.getUserID();
                            if (map.containsKey(sUserID)) {
                                Order order1 = map.get(sUserID);
                                map.get(sUserID).setOrderTotalPrice(order1.getOrderTotalPrice() + newOrder.getOrderTotalPrice());
                                map.get(sUserID).setOrderCount(order1.getOrderCount() + newOrder.getOrderCount());
                            } else {
                                map.put(newOrder.getUserID(), newOrder);
                            }
                        },

                        //第三个参数,实现并行计算
                        (HashMap<String, Order> map1, HashMap<String, Order> map2) -> {
                            System.out.println("并行逻辑");
                            map2.forEach((key, value) -> {
                                map1.merge(key, value, (order1, order2) -> {
                                    return new Order(0, order1.getOrderCount() + order2.getOrderCount(), order1.getOrderTotalPrice() + order2.getOrderTotalPrice(), key);
                                });
                            });
                        });
        System.out.println(JSONObject.toJSONString(colletMap,true));
    }

 

 

 

打印日志:

初始化逻辑
计算逻辑
计算逻辑
计算逻辑
{
    "lisi":{
        "id":1,
        "orderCount":1,
        "orderTotalPrice":11.0,
        "userID":"lisi"
    },
    "zhangsan":{
        "id":1,
        "orderCount":5,
        "orderTotalPrice":109.0,
        "userID":"zhangsan"
    }
}

Process finished with exit code 0

 

posted on 2020-05-10 08:28  菜鸟乙  阅读(183)  评论(0编辑  收藏  举报