### 在一個集合中垛孔, 根據(jù)對象的某個屬性進(jìn)行去重
List<InquiryCoatedProductDTO> resultList = list.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(inquiryCoatedProductDTO -> inquiryCoatedProductDTO.getMID()))), ArrayList::new)
);
### 根據(jù)多個屬性同時去重
List<Holiday> result = set.stream().collect(
Collectors. collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId() + ";" + o.getHolidayType()))), ArrayList::new)
);
### 將相同id或者相同標(biāo)志性字段狈邑,進(jìn)行累加
如:validRecipientDetailList = [
{productId: '1001', recipientNum: 100},
{productId: '1001', recipientNum: 200},
{productId: '1002', recipientNum: 200}
]
輸出結(jié)果為: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())));
### 集合轉(zhuǎn)數(shù)組
list.toArray(new String[list.size()])
## List<Map<String, Object>> 進(jìn)行排序指巡,正序和倒序排序,只需更改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>結(jié)構(gòu)進(jìn)行分組排序統(tǒng)計稳摄,并進(jìn)行倒序排序
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));
## 循環(huán)饲宿,取出前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);
}
## 根據(jù)某字段進(jìn)行求和
Map<String, Double> groupMap = financeOrderBillList.stream()
.collect(Collectors.groupingBy(FinanceOrderBillDTO::getCustomerCompanyName, Collectors.summingDouble(FinanceOrderBillDTO::getBillMoney)));
## Map根據(jù)某字段進(jìn)行排序
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));