zoukankan      html  css  js  c++  java
  • JAVA 8 STREAM

    package japan.example.test;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    import lombok.Data;
    
    public class StreamTests {
    
        public static void main(String[] args) {
    
            StreamTests test = new StreamTests();
    
            test.test(test.data());
        }
    
        public List<ResponseData> data() {
            ResponseData data = new ResponseData();
    
            data.serialno = "0011";
            data.setStatus("succ");
    
            List<ResponseData> list = new ArrayList<>();
            list.add(data);
    
            data = new ResponseData();
            data.setSerialno("0011");
            data.setStatus("fail");
    
            list.add(data);
    
            data = new ResponseData();
            data.setSerialno("0012");
            data.setStatus("wait");
    
            list.add(data);
            return list;
        }
    
        public void test(List<ResponseData> dataarray) {
            Map<String, List<String>> counting = dataarray.stream().
                    collect(Collectors.groupingBy(ResponseData::getSerialno,
                    Collectors.mapping(ResponseData::getStatus, Collectors.toList())));
    
            Map<String, List<ResponseData>> conuntink = dataarray.stream()
                    .collect(Collectors.groupingBy(ResponseData::getSerialno, Collectors.toList()));
    
            counting.entrySet().forEach(x -> {
                String key = x.getKey();
                List<String> val = x.getValue();
    
                System.err.println(key + val);
            });
        }
    }
    
    @Data
    class ResponseData {
        String serialno;
        String status;
    }
    @Data
    class Item {
        Long id;
        int count;
        BigDecimal price;
    
        public Item(Long id, int count, BigDecimal price) {
            this.id = id;
            this.count = count;
            this.price = price;
        }
    
        public static void main(String[] args) {
            List<Item> items = Arrays.asList(//
                    new Item(1000L, 2, new BigDecimal("10.00")), //
                    new Item(1001L, 2, new BigDecimal("11.00")), //
                    new Item(1001L, 3, new BigDecimal("11.00")), //
                    new Item(1002L, 2, new BigDecimal("12.00")), //
                    new Item(1002L, 2, new BigDecimal("12.00")), //
                    new Item(1002L, 2, new BigDecimal("12.00")));
    
            Map<Long, List<BigDecimal>> datas = items.stream().collect(Collectors.groupingBy(Item::getId, Collectors.mapping(Item::getPrice, Collectors.toList())));
            System.err.println(datas);
    
            Map<Long, BigDecimal> prices = items.stream().collect(Collectors.groupingBy(Item::getId, Collectors.mapping(Item::getPrice, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
            System.err.println(prices);
        }
    
    }
  • 相关阅读:
    《精通情绪》读书笔记
    绩效评估与绩效反馈
    数据分析报告格式zz
    学会谈判zz
    javascript中的双向绑定
    理解 DocumentFragment
    理解Object.defineProperty()
    Vue 改变数组中对象的属性不重新渲染View的解决方案
    ES6中Object.assign() 方法
    控制input输入框光标的位置
  • 原文地址:https://www.cnblogs.com/jpit/p/8360808.html
Copyright © 2011-2022 走看看