Java8的stream處理List集合的相同部分(交集)、去重
點(diǎn)擊上方“程序員大白”,選擇“星標(biāo)”公眾號(hào)
重磅干貨,第一時(shí)間送達(dá)

Java8的新特性——Stream常用于處理集合,它不會(huì)改變集合原有的結(jié)構(gòu),優(yōu)點(diǎn)是Stream的代碼會(huì)比用for循環(huán)處理簡(jiǎn)潔不少
本文主要說(shuō)的是:獲取兩個(gè)List集合的交集、差集、去重
文章目錄
兩個(gè)集合的交集 差集 去重 list.stream()是構(gòu)造方法
一、兩個(gè)集合的交集
例如:找出兩個(gè)班 名字相同的學(xué)生
public?class?Student?{
?
?private?String?studentNo;
?//名字
????private?String?studentName;
?
?public?Student(String?studentNo,?String?studentName)?{
????????this.studentNo?=?studentNo;
????????this.studentName?=?studentName;
????}
?
?//對(duì)象的比較涉及到equals()的重寫,?這里僅僅比較studentName是否相同
?@Override
????public?boolean?equals(Object?o)?{
????????if?(this?==?o)?return?true;
????????if?(!(o?instanceof?Student))?return?false;
????????Student?student?=?(Student)?o;
????????return?studentName.equals(student.getStudentName());
????}
?
?//?set()和get()方法均省略..
}
學(xué)生是個(gè)對(duì)象實(shí)例,我們要比較的是名字是否相同,僅需要重寫equals()方法即可
找交集
@Test
public?void?test(){
?//?1班的學(xué)生
?List?class01=new?ArrayList<>();
????class01.add(new?Student("1","小明"));
????class01.add(new?Student("2","趙鐵柱"));?
?
?//?2班的學(xué)生
?List?class02=new?ArrayList<>();
????class02.add(new?Student("1","趙鐵柱"));
?//?找兩個(gè)班名字相同的同學(xué)(取交集),比較用的是重寫的equals()
?List?sameName=class01.stream().filter(class02::contains).collect(Collectors.toList());
?sameName.stream().forEach(student->System.out.println(student.getStudentName()+"?"));
?
?//output?:?趙鐵柱
}????
需要注意的是:(1) class01.stream().filter(class02::contains)的filter()會(huì) 保留 符合表達(dá)式的結(jié)果,這里面表達(dá)式的內(nèi)容是 2班和1班名字相同的同學(xué)
(2) forEach是遍歷集合,代替了for循環(huán),代碼更為簡(jiǎn)潔
(3) collect(Collectors.toList())、collect(Collectors.toSet())、collect(Collectors.toMap())將Stream的數(shù)據(jù)歸集到List、Map、Set等集合
二、差集
輸出結(jié)果:b c
@Test
public?void?test(){
?List?list01=Arrays.asList("a","b","c");
????List?list02=Arrays.asList("a","e","f");
?
?//list01和list02的差集,?僅保留了?b,c
????List?result=list01.stream().filter(word->!list02.contains(word)).collect(Collectors.toList());
????result.stream().forEach(word->System.out.print(word+"?"));
}
表達(dá)式 list01.stream().filter(word-> ! list02.contains(word)),要找的元素,它的特征是只存在list01中,但不存在list02中,! list02.contains(word)就是說(shuō)這個(gè)元素不在list02中
三、去重
輸出結(jié)果:a b c
List?list=Arrays.asList("a","b","c","a");
List?distinct=list.stream().distinct().collect(Collectors.toList());
distinct.stream().forEach(word->System.out.print(word+"?"));?
刪除了重復(fù)的字符"a"
四、list.stream()是構(gòu)造方法
可能有朋友對(duì)list.stream()有些疑惑,它是個(gè)Stream的構(gòu)造方法,Stream的構(gòu)造方法如下:
(1) 用集合創(chuàng)建Stream
List?list=Arrays.asList("a","b","c");
//創(chuàng)建順序流
Stream?stream=list.stream();
//創(chuàng)建并行流
Stream?parallelStream=list.parallelStream();
(2) 用數(shù)組Arrays.stream(array)創(chuàng)建Stream
int[]?array={1,2,3,4,5};
IntStream?stream=Arrays.stream(array);
(3) 用Stream創(chuàng)建Stream
Stream?stream=Stream.of(1,2,3,4,5);
常用的是上面這三種,另外還有iterate(),generate(),后者是生成隨機(jī)數(shù),兩個(gè)構(gòu)造方法均產(chǎn)生無(wú)限流(即元素的個(gè)數(shù)是無(wú)限的)。
如果要求數(shù)量有限,則需要用 limit 來(lái)限制,如:
Stream?stream=Stream.iterate(0,num->num+3).limit(10)
打印了[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
來(lái)源:blog.csdn.net/qq_44384533/article/
details/113883652
推薦閱讀
關(guān)于程序員大白
程序員大白是一群哈工大,東北大學(xué),西湖大學(xué)和上海交通大學(xué)的碩士博士運(yùn)營(yíng)維護(hù)的號(hào),大家樂于分享高質(zhì)量文章,喜歡總結(jié)知識(shí),歡迎關(guān)注[程序員大白],大家一起學(xué)習(xí)進(jìn)步!

