- 写在前面
- 本来以为stream应该有类似这种语法,想这样写的,可惜事与愿违,需要开发者换种思路。
List acceptances = vo.stream().filter(distinct(b -> b.getProjectId())).collect(Collectors.toList());
- 自带的
distinct
似乎只能将所有字段都相同的数据去重,若不是还请大佬们在评论区指导下。
List acceptances = vo.stream().distinct().collect(Collectors.toList());
方法一:自带方法Comparator.comparing(p -> p.get***())
String sql = "";
List<ProjectInfoVo> vo = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ProjectInfoVo.class));
ArrayList<ProjectInfoVo> collect = vo.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(p -> p.getProjectId()))), ArrayList::new));
String sql = "";
List<ProjectInfoVo> vo = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ProjectInfoVo.class));
ArrayList<ProjectInfoVo> collect = vo.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(p -> p.getProjectId()+";"+p.getMember()))), ArrayList::new));
方法二:自定义方法Comparator.comparing(p -> p.get***())
2.1 自定义方法类——distinctByKey
public class StreamUtils {
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
2.2 使用示例
List<ProjectInfoVo> acceptances = vo.stream()
.filter(StreamUtils.distinctByKey(b -> b.getProjectId()))
.collect(Collectors.toList());
List<ProjectInfoVo> acceptances = vo.stream()
.filter(StreamUtils.distinctByKey(b -> b.getProjectId()))
.filter(StreamUtils.distinctByKey(b -> b.getMember()))
.collect(Collectors.toList());