### 在一个集合中, 根据对象的某个属性进行去重
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));