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