java stream distinct() 按一个或多个指定对象字段进行去重

  • 写在前面
    • 本来以为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 {

    /**
     * 按某个指定字段去重
     *
     * @param keyExtractor 需要去重的字段
     * @return java.util.function.Predicate
     * @author 陈賝
     * @date 2022/9/2 13:49
     */
    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());

你可能感兴趣的:(Java,java,开发语言)