lamba表達式使用技巧

### 在一個集合中, 根據對象的某個屬性進行去重
List<InquiryCoatedProductDTO> resultList = list.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(inquiryCoatedProductDTO -> inquiryCoatedProductDTO.getMID()))), ArrayList::new)
        );

### 根據多個屬性同時去重
List<Holiday> result = set.stream().collect(
                Collectors. collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId() + ";" + o.getHolidayType()))), ArrayList::new)
        );
### 將相同id或者相同標志性字段,進行累加
如:validRecipientDetailList = [
    {productId: '1001', recipientNum: 100},
    {productId: '1001', recipientNum: 200},
    {productId: '1002', recipientNum: 200}
]
輸出結果為:filterList = [
    {productId: '1001', recipientNum: 300}, 
    {productId: '1002', recipientNum: 200}
]
 List<ValidRecipientDetailDTO> filterList = validRecipientDetailList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toMap(ValidRecipientDetailDTO::getProductId, Function.identity(), (left, right) -> {
                    left.setRecipientNum(left.getRecipientNum() + right.getRecipientNum());
                    return left;
                }), m -> new ArrayList<>(m.values())));
### 集合轉數組
list.toArray(new String[list.size()])
## List<Map<String, Object>> 進行排序,正序和倒序排序,只需更改name1和name2的順序
Collections.sort(resultMapList, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Double name1 = Double.valueOf(o1.get("value").toString());
                Double name2 = Double.valueOf(o2.get("value").toString());
                return name2.compareTo(name1);
            }
        });
## Map<String,Long>結構進行分組排序統計,并進行倒序排序
 Map<String, Long> groupMap = orderDetailList.stream()
                .collect(Collectors.groupingBy(OrderDetailDTO::getCategoryUuid, Collectors.counting()));
Map<String, Long> sortedMap = new TreeMap<>(Comparator.comparing(groupMap::get, Comparator.reverseOrder()));
sortedMap.putAll(groupMap);
## 倒序后,取前面的10條
Map<String, Long> top10Records = sortedMap.entrySet().stream()
                .limit(10) // 限制前10條記錄
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
## 循環,取出前10條記錄,放入到resutltMapList中
List<Map<String, Object>> resultMapList = new LinkedList<>();
for (Map.Entry<String, Double> entry : top10Records.entrySet()) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("title", entry.getKey());
            map.put("value", entry.getValue());
            resultMapList.add(map);
}
## 根據某字段進行求和
 Map<String, Double> groupMap = financeOrderBillList.stream()
                .collect(Collectors.groupingBy(FinanceOrderBillDTO::getCustomerCompanyName, Collectors.summingDouble(FinanceOrderBillDTO::getBillMoney)));
## Map根據某字段進行排序
Map<String, PlateCategoryDTO> mlist = new TreeMap<String, PlateCategoryDTO>();
mlist = mlist.entrySet().stream()
                            .sorted(Map.Entry.comparingByValue(comparingInt(PlateCategoryDTO::getPosition)))
                            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                                    (oldValue, newValue) -> oldValue, LinkedHashMap::new));
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容