• 工作中使用过与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
  • 相关阅读:
    网络工程师常用高级技术,路由策略全面详解
    DC电源模块对效率有什么要求?
    GraalVM入门教程
    一文教你处理SpringBoot统一返回格式
    流量主接入广告:实现盈利与用户体验的平衡
    anaconda 安装 pytorch 和 tensorflow
    C++ vector 的模拟实现
    Pytorch知识点学习
    信息系统项目管理师---第十一章项目风险管理
    golang之context实用记录
  • 原文地址:https://blog.csdn.net/qq_44377709/article/details/126463446