java8根據(jù)某一屬性過(guò)濾去重的實(shí)例
java8根據(jù)某一屬性過(guò)濾去重
最近小編剛接觸到j(luò)ava8特性,在不知道有java8特性的時(shí)候,一個(gè)for循環(huán)套一個(gè)for循環(huán),自從接觸大java8,為自己省了很多事,節(jié)省了很多代碼量.
根據(jù)list某一屬性去重
//根據(jù)id去重 examRoomModelLists = examRoomModelLists.stream().collect(Collectors.collectingAndThen(Collectors.toCollection( ? ? ? ? ? ? ? ? // 利用 TreeSet 的排序去重構(gòu)造函數(shù)來(lái)達(dá)到去重元素的目的 ? ? ? ? ? ? ? ? // 根據(jù)firstName去重 ? ? ? ? ? ? ? ? () -> new TreeSet<>(Comparator.comparing(ExamRoomModel::getId))), ArrayList::new));
過(guò)濾StudentExamState=0的數(shù)據(jù)
em.setNoLoginExamineeCount((examinee.stream().map(ExamineeEntity::getStudentExamState).filter(x ->? x == 0).collect(Collectors.toList())).size()); ? ? ? ? ? ? }
過(guò)濾ExamRoomStudentCount=0的數(shù)據(jù)
?List<ExamRoomModel> filterList = examRoomModelLists.stream().filter(ExamRoomModel ->? ?!Objects.equals(ExamRoomModel.getExamRoomStudentCount(), 0)).collect(Collectors.toList());
是不是很方便,換成以前過(guò)濾去重不知道要寫(xiě)多少橫代碼,現(xiàn)在一行解決.
Java8 stream根據(jù)對(duì)象字段去重
public class Java8StreamTest {
public static class Book{
private String id;
private String name;
public Book(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
@Test
public void testUnique(){
List<Book> books = Lists.newArrayList(new Book("1","1"),new Book("2","2"),new Book("3","3"),new Book("2","2"));
//使用TreeSet去重
List<Book> unique1 = books.stream().collect(
collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))),
ArrayList::new));
System.out.println(unique1);
//使用map去重
List<Book> unique2 = books.stream()
.filter(distinctByKey(o -> o.getId()))
.collect(Collectors.toList());
System.out.println(unique2);
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
System.out.println("這個(gè)函數(shù)將應(yīng)用到每一個(gè)item");
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
stream對(duì)list中的對(duì)象進(jìn)行去重
首先我們有一個(gè)對(duì)象屬性如下
@Data
public class Person {
? ? private String id;
? ? private String name;
? ? private String sex;
}我們根據(jù)屬性name來(lái)去重,去重代碼如下
List<Person> persons = new ArrayList(); //賦值初始化過(guò)程省略 List<Person> uniqueByName = persons.stream().collect( ? ? ? ? ? ? Collectors.collectingAndThen( ? ? ? ? ? ? ? ? ? ? Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new) );
根據(jù)name,sex兩個(gè)屬性去重
List<Person> persons = new ArrayList(); //賦值初始化過(guò)程省略 List<Person> uniqueByNameAndSex = persons.stream().collect( ? ? ? ? ? ?Collectors. collectingAndThen( ? ? ? ? ? ? ? ? ? ? Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new) );
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring?aop?pointcut?添加多個(gè)execution方式
這篇文章主要介紹了spring?aop?pointcut?添加多個(gè)execution方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
elasticsearch節(jié)點(diǎn)間通信的基礎(chǔ)transport啟動(dòng)過(guò)程
這篇文章主要為大家介紹了elasticsearch節(jié)點(diǎn)間通信的基礎(chǔ)transport啟動(dòng)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
java 抓取網(wǎng)頁(yè)內(nèi)容實(shí)現(xiàn)代碼
這篇文章主要介紹了java 抓取網(wǎng)頁(yè)內(nèi)容實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02
java實(shí)現(xiàn)socket客戶(hù)端連接服務(wù)端
本文是個(gè)人剛剛開(kāi)始學(xué)習(xí)如何通過(guò)socket去發(fā)送信息下邊的案例,也是書(shū)上的在這留下筆記,最后附上一個(gè)實(shí)例,有需要的小伙伴可以參考下。2015-10-10
Spring?Boot?優(yōu)雅整合多數(shù)據(jù)源
這篇文章主要介紹了Spring?Boot?優(yōu)雅整合多數(shù)據(jù)源,多數(shù)據(jù)源就是在一個(gè)單一應(yīng)用中涉及到了兩個(gè)及以上的數(shù)據(jù)庫(kù),更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章介紹2022-05-05

