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à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)文章
SpringBoot中過(guò)濾器Filter+JWT令牌實(shí)現(xiàn)登錄驗(yàn)證
本文主要介紹了SpringBoot中過(guò)濾器Filter+JWT令牌實(shí)現(xiàn)登錄驗(yàn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
MyBatis-Plus中如何使用ResultMap的方法示例
本文主要介紹了MyBatis-Plus中如何使用ResultMap,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP軟件遠(yuǎn)程窗口實(shí)現(xiàn)(6)
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件,F(xiàn)TP軟件遠(yuǎn)程窗口的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
java工具類SendEmailUtil實(shí)現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了java工具類SendEmailUtil實(shí)現(xiàn)發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
SpringAop中AspectJ框架的切入點(diǎn)表達(dá)式
這篇文章主要介紹了SpringAop中AspectJ框架的切入點(diǎn)表達(dá)式,AspectJ是一個(gè)基于Java語(yǔ)言的AOP框架,Spring2.0以后新增了對(duì)AspectJ切點(diǎn)表達(dá)式支持,@AspectJ 是AspectJ1.5新增功能,通過(guò)JDK5注解技術(shù),允許直接在Bean類中定義切面,需要的朋友可以參考下2023-08-08
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個(gè)組件,在整個(gè)生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級(jí)功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
jackson json序列化實(shí)現(xiàn)首字母大寫,第二個(gè)字母需小寫
這篇文章主要介紹了jackson json序列化實(shí)現(xiàn)首字母大寫,第二個(gè)字母需小寫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

