java?stream?distinct()如何按一個或多個指定對象字段進行去重
更新時間:2023年12月29日 09:13:53 作者:陳賝
這篇文章主要介紹了java?stream?distinct()如何按一個或多個指定對象字段進行去重問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
寫在前面
本來以為stream應(yīng)該有類似這種語法,想這樣寫的,可惜事與愿違,需要開發(fā)者換種思路。
List<ProjectInfoVo> acceptances = vo.stream().filter(distinct(b -> b.getProjectId())).collect(Collectors.toList());
自帶的 distinct 似乎只能將所有字段都相同的數(shù)據(jù)去重,若不是還請大佬們指導(dǎo)。
List<ProjectInfoVo> 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***())
1.自定義方法類——distinctByKey
public class StreamUtils {
/**
* 按某個指定字段去重
*
* @param keyExtractor 需要去重的字段
* @return java.util.function.Predicate<T>
* @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.使用示例
- 單個字段
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());
方法三:Stream API
1.Collectors.toMap
- 單個字段
private static List<CompanyVo> mergeAndDistinct(List<CompanyVo> companyVoList) {
// return companyVoList.stream().collect(Collectors.collectingAndThen(
// Collectors.toCollection(() -> new TreeSet<>(
// Comparator.comparing(CompanyVo::getId))), ArrayList::new));
return new ArrayList<>(companyVoList.stream()
.collect(Collectors.toMap(
CompanyVo::getId, // 以 id 作為 key
companyVo -> companyVo, // 保留 CompanyVo 對象
(existing, replacement) -> existing // 如果有重復(fù)的 id,保留已有的對象
)).values());
}
- 多個字段
private static List<CompanyVo> mergeAndDistinct(List<CompanyVo> companyVoList) {
return new ArrayList<>(companyVoList.stream()
.collect(Collectors.toMap(
companyVo -> companyVo.getId() + "-" + companyVo.getCode(), // 使用 id 和 code 作為 key
companyVo -> companyVo, // 保留 CompanyVo 對象
(existing, replacement) -> existing // 如果有重復(fù)的 id,保留已有的對象
)).values());
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實現(xiàn)學(xué)生信息管理系統(tǒng)(借助Array?List)
這篇文章主要為大家詳細介紹了Java實現(xiàn)學(xué)生信息管理系統(tǒng),借助Array?List,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
SpringBoot返回多種格式的數(shù)據(jù)的實現(xiàn)示例
本文主要介紹了SpringBoot返回多種格式的數(shù)據(jù)的實現(xiàn)示例,主要包括了FastJson,xml,pdf,excel,資源流,具有一定的參考價值,感興趣的可以了解一下2021-10-10

