美文网首页
一、JAVA去重

一、JAVA去重

作者: maololo | 来源:发表于2018-09-19 09:14 被阅读22次
  • 版本Java8

1、字符串集合去重

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

2、根据对象属性去重

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))

相关文章

网友评论

      本文标题:一、JAVA去重

      本文链接:https://www.haomeiwen.com/subject/mdybnftx.html