有一個List<Map<String,Object>> list, 里面為獲取到的流水,由於一條流水需要分開幾次操作,現需要對list中map根據流水號去
//得到所有流水號 去重
List<Object> collect = allList.stream().map(m -> m.get("ca00")).distinct().collect(Collectors.toList());
ArrayList<Map<String, Object>> maps = new ArrayList<>();
collect.forEach(co -> {//遍歷 根據流水號獲取流水
Map<String, Object> map = allList.stream().filter(al -> Objects.equals(al.get("ca00"), co)).findFirst().get();
maps.add(map);
});
找了一種優雅的寫法
list = list.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(da -> da.get("ORDTB407CA00").toString()))),//根據訂單號去重
ArrayList::new));
仔細看了一下,發現 collectingAndThen 是先收集結果,然后對收集到的結果做后面 Function 函數的操作
例如下面代碼 對收納的結果進行拼接,然后將拼接結果轉大寫
String collect = Stream.of("aoe", "uid", "htn").collect(Collectors.collectingAndThen(Collectors.joining("-"), String::toUpperCase));
