java8根據(jù)某一屬性過濾去重的實例
更新時間:2022年05月06日 11:13:42 作者:檸檬草。
這篇文章主要介紹了java8根據(jù)某一屬性過濾去重的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
java8根據(jù)某一屬性過濾去重
最近小編剛接觸到java8特性,在不知道有java8特性的時候,一個for循環(huán)套一個for循環(huán),自從接觸大java8,為自己省了很多事,節(jié)省了很多代碼量.
根據(jù)list某一屬性去重
//根據(jù)id去重 examRoomModelLists = examRoomModelLists.stream().collect(Collectors.collectingAndThen(Collectors.toCollection( ? ? ? ? ? ? ? ? // 利用 TreeSet 的排序去重構造函數(shù)來達到去重元素的目的 ? ? ? ? ? ? ? ? // 根據(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ù)將應用到每一個item");
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
stream對list中的對象進行去重
首先我們有一個對象屬性如下
@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) );
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
spring?aop?pointcut?添加多個execution方式
這篇文章主要介紹了spring?aop?pointcut?添加多個execution方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
elasticsearch節(jié)點間通信的基礎transport啟動過程
這篇文章主要為大家介紹了elasticsearch節(jié)點間通信的基礎transport啟動過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
Spring?Boot?優(yōu)雅整合多數(shù)據(jù)源
這篇文章主要介紹了Spring?Boot?優(yōu)雅整合多數(shù)據(jù)源,多數(shù)據(jù)源就是在一個單一應用中涉及到了兩個及以上的數(shù)據(jù)庫,更多相關內容需要的小伙伴可以參考下面文章介紹2022-05-05

