欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學堂Java培訓之list按照元素某個字段去重詳解

2019-06-19 13:25:24 2252瀏覽

今天千鋒扣丁學堂Java培訓老師給大家分享一篇關(guān)于如何實現(xiàn)Java8中l(wèi)ist按照元素的某個字段去重的詳細介紹,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,下面我們一起來看一下吧。



list按照元素的某個字段去重

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer age;
private String name;
}

測試數(shù)據(jù)

List<Student> studentList = Lists.newArrayList();
studentList.add(new Student(28, "river"));
studentList.add(new Student(12, "lucy"));
studentList.add(new Student(33, "frank"));
studentList.add(new Student(33, "lucy"));

java8通過treeset去重

List<Student> studentDistinctList = studentList.stream()
.collect(Collectors.collectingAndThen
(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(t -> t.getName()))),
ArrayList::new
)
);
System.out.println(new Gson().toJson(studentDistinctList));

擴展distinct方法去重

List<Student> studentDistinct2List = studentList.stream().filter(StreamUtil.distinctByKey(t->t.getName()))
.collect(Collectors.toList());
System.out.println(new Gson().toJson(studentDistinct2List));

工具類

public class StreamUtil {
/**
* https://stackoverflow.com/questions/23699371/java-8-distinct-by-property
* @param keyExtractor
* @param <T>
* @return
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
}

以上就是關(guān)于千鋒扣丁學堂Java培訓之list按照元素某個字段去重詳解的全部內(nèi)容,希望對大家的學習有所幫助,想要了解更多關(guān)于Java開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學堂Java培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育有專業(yè)的Java講師為您指導,此外扣丁學堂老師精心推出的Java視頻教程定能讓你快速掌握Java從入門到精通開發(fā)實戰(zhàn)技能??鄱W堂Java技術(shù)交流群:850353792。


                        JavaEE/微服務/源碼解析/分布式/企業(yè)級架構(gòu)【VIP體驗課】


     【關(guān)注微信公眾號獲取更多學習資料】       【掃碼進入JavaEE/微服務VIP免費公開課】  



查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>

標簽: Java培訓 Java視頻教程 Java多線程 Java面試題 Java學習視頻 Java開發(fā)

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部