<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java8的stream處理List集合的相同部分(交集)、去重

          共 2994字,需瀏覽 6分鐘

           ·

          2022-01-17 02:33

          點(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 of(T... values)創(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


          13個(gè)你一定要知道的PyTorch特性

          解讀:為什么要做特征歸一化/標(biāo)準(zhǔn)化?

          一文搞懂 PyTorch 內(nèi)部機(jī)制

          張一鳴:每個(gè)逆襲的年輕人,都具備的底層能力


          關(guān)


          ,學(xué),西學(xué)學(xué)運(yùn)營(yíng)護(hù)號(hào),質(zhì)結(jié)識(shí),關(guān)[],學(xué)習(xí)進(jìn)!


          瀏覽 96
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩成人免费大片 | 亚洲欧美色图在线 | 国产亚洲视频在线观看 | 国产亚洲激情 | 中文字幕在线观看第一页2019 |