• 工作中使用过与stream流相关的API操作


    简单记录工作中使用过与stream流相关的API操作


    一、List

    1、合并list中的key-value数据
    public static Map<String, String> mergeMap(List<Map<String, String>> dataList) {
    
        return dataList.stream()
                .map(Map::entrySet)
                .flatMap(Set::stream)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7



    2、按单个key进行分组
    List<Integer> numberList = Arrays.asList(3, 5, 6, 4, 2, 8, 9, 1, 7);
    
    //使用stream流进行分组(使用条件)
    Map<Boolean, List<Integer>> splitMap = numberList
        .stream()
        .collect(Collectors.groupingBy(number -> number < 5));
    
    // 满足条件的一组
    List<Integer> trueList = splitMap.get(true);
    // 不满足条件的一组
    List<Integer> falseList = splitMap.get(false);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11



    3、按照对象(BookEntry)指定属性进行分组
    List<BookEntry> newEntryList = groupBookEntryList(entryList)
    
    
    private List<BookEntry> groupBookEntryList(List<BookEntry> oldEntryList) {
    
    	// 按照指定列进行分组
        Map<String, List<BookEntry>> afterGroupEntryList = oldEntryList.stream()
                .collect(Collectors.groupingBy(AcctTotalCon::fetchGroupKey));
    
    	// 将分组后的数据重新处理
        afterGroupEntryList.forEach((key, value) -> {
            BookEntry newEntry = new BookEntry();
            String[] split = key.split("\\|", -1);
            newEntry.setOrgId(split[0]);
            newEntry.setCode(split[1]);
            newEntry.setCur(split[2]);
    
            // 分组中所有的金额相加为一组
            AtomicReference<Double> amt = new AtomicReference<>(0D);
            value.forEach(entry -> {
                // 红字,增加负数
                Double t = entry.getValue();
                amt.set(CalculatorUtil.add(amt.get(), t));
    
            });
            newEntry.setValue(amt.get());
    
            retEntryList.add(newEntry);
        });
    }
    
    
    // 按照orgId、code、cur三个属性进行分组
    private static String fetchGroupKey(BookEntry entry) {
        return entry.getOrgId() + "|" + entry.getCode() + "|"
                + entry.getCur();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37




    二、Map

    1、Map --> Map
    public static Map<String, Object> strMapToObjMap(Map<String, String> stringMap) {
        return stringMap.entrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey, entry -> (Object) entry.getValue()));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5



    2、对map指定K或者V进行排序
    Map<String, Integer> testMap = Maps.newHashMap();
    testMap.put("g", 2);
    testMap.put("b", 1);
    testMap.put("c", 2);
    testMap.put("a", 9);
    testMap.put("k", 3);
    Map<String, Integer> keySortMap = new LinkedHashMap<>();
    Map<String, Integer> valueSortMap = new LinkedHashMap<>();
    
    testMap.entrySet().stream()
            .sorted(Map.Entry.comparingByKey())
            .forEachOrdered(item -> keySortMap.put(item.getKey(), item.getValue()));
    
    
    testMap.entrySet().stream()
            .sorted(Map.Entry.comparingByValue())
            .forEachOrdered(item -> valueSortMap.put(item.getKey(), item.getValue()));
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    UE4 C++设计模式:策略模式(Strategy Pattern)
    基于微信小程序的奶茶在线预定点单管理系统
    JAVA毕业设计家教信息管理系统计算机源码+lw文档+系统+调试部署+数据库
    二十、泛型(1)
    直播交友app开发_1对1视频直播聊天APP定制_语音直播交友软件源码
    GALIL运动控制卡维修控制器维修DMC-1840
    实现Web API版本控制-springboot
    [网络安全]实操DVWS靶场复现CSRF漏洞
    pdf压缩文件怎么压缩最小?
    机器学习的基本代码
  • 原文地址:https://blog.csdn.net/qq_44377709/article/details/126463446