字符串集合去重
List<String> distinctElements = list.stream().distinct().collect(Collectors.toList());
根據(jù)對象屬性去重
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
{
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
//使用舉例
persons.stream().filter(distinctByKey(Person::getName))