Java8优雅去重

字符串集合去重

List distinctElements = list.stream().distinct().collect(Collectors.toList());

根据对象属性去重

public static  Predicate distinctByKey(Function keyExtractor)
    {
        Map map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
//使用举例
persons.stream().filter(distinctByKey(Person::getName))

你可能感兴趣的:(Java8优雅去重)