lamba表达式使用技巧

### 在一个集合中, 根据对象的某个属性进行去重
List resultList = list.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(inquiryCoatedProductDTO -> inquiryCoatedProductDTO.getMID()))), ArrayList::new)
        );

### 根据多个属性同时去重
List 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 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()])

你可能感兴趣的:(lamba表达式使用技巧)