java?stream?distinct()如何按一個(gè)或多個(gè)指定對(duì)象字段進(jìn)行去重
寫在前面
本來以為stream應(yīng)該有類似這種語法,想這樣寫的,可惜事與愿違,需要開發(fā)者換種思路。
List<ProjectInfoVo> acceptances = vo.stream().filter(distinct(b -> b.getProjectId())).collect(Collectors.toList());
自帶的 distinct 似乎只能將所有字段都相同的數(shù)據(jù)去重,若不是還請(qǐng)大佬們指導(dǎo)。
List<ProjectInfoVo> acceptances = vo.stream().distinct().collect(Collectors.toList());
方法一:自帶方法
Comparator.comparing(p -> p.get***())
- 單個(gè)字段
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));
- 多個(gè)字段
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 {
/**
* 按某個(gè)指定字段去重
*
* @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.使用示例
- 單個(gè)字段
List<ProjectInfoVo> acceptances = vo.stream() .filter(StreamUtils.distinctByKey(b -> b.getProjectId())) .collect(Collectors.toList());
- 多個(gè)字段
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
- 單個(gè)字段
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 對(duì)象
(existing, replacement) -> existing // 如果有重復(fù)的 id,保留已有的對(duì)象
)).values());
}
- 多個(gè)字段
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 對(duì)象
(existing, replacement) -> existing // 如果有重復(fù)的 id,保留已有的對(duì)象
)).values());
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
CentOS7和8中安裝Maven3.8.4的簡(jiǎn)單步驟
maven是屬于apache的一個(gè)工具,主要是對(duì)java進(jìn)行編譯打包,解決依賴關(guān)系,下面這篇文章主要給大家介紹了關(guān)于CentOS7和8中安裝Maven3.8.4的相關(guān)資料,需要的朋友可以參考下2022-04-04
Java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(借助Array?List)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),借助Array?List,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
java計(jì)算代碼段執(zhí)行時(shí)間的詳細(xì)代碼
java里計(jì)算代碼段執(zhí)行時(shí)間可以有兩種方法,一種是毫秒級(jí)別的計(jì)算,另一種是更精確的納秒級(jí)別的計(jì)算,這篇文章主要介紹了java計(jì)算代碼段執(zhí)行時(shí)間,需要的朋友可以參考下2022-08-08
Netty源碼分析NioEventLoop線程的啟動(dòng)
這篇文章主要為大家介紹了Netty源碼分析NioEventLoop線程的啟動(dòng)示例,有需要的朋友,可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
SpringBoot返回多種格式的數(shù)據(jù)的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot返回多種格式的數(shù)據(jù)的實(shí)現(xiàn)示例,主要包括了FastJson,xml,pdf,excel,資源流,具有一定的參考價(jià)值,感興趣的可以了解一下2021-10-10

