liusheng
21 小时以前 459aa78c84cf552ebea6ef056d978c2531d71ac8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.smartor.common;
 
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * 去重工具类
 */
public class DistinctByProperty {
 
    // 创建去重工具方法,根据集合中上属性
    public static <T> List<T> distinctByProperty(
            List<T> list,
            Function<? super T, ?> keyExtractor) {
 
        Set<Object> seen = new HashSet<>();
        return list.stream()
                .filter(item -> seen.add(keyExtractor.apply(item)))
                .collect(Collectors.toList());
    }
}