<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 排序以及自定義比較器,很實(shí)用!

          共 502字,需瀏覽 2分鐘

           ·

          2022-04-24 08:38

          你知道的越多,不知道的就越多,業(yè)余的像一棵小草!

          你來,我們一起精進(jìn)!你不來,我和你的競爭對(duì)手一起精進(jìn)!

          編輯:業(yè)余草

          blog.csdn.net/qq_31635851?

          推薦:https://www.xttblog.com/?p=5338

          閱讀本文大概需要 6 分鐘。

          在本頁中,我們將提供 java 8 Stream sorted()排序的示例。我們可以按照自然順序和比較器提供的順序?qū)α鬟M(jìn)行排序。

          在Java8中,可以使用lambda表達(dá)式實(shí)例化比較器(Comparator)。

          我們還可以顛倒自然順序和比較器(Comparator)提供的順序。

          自然排序使用Comparable提供的排序,該排序必須由實(shí)例為流元素的類實(shí)現(xiàn)。

          在本頁中,我們將使用java 8 Stream sorted()方法對(duì)列表List,Map和Set進(jìn)行排序。

          使用Stream sorted

          使用Stream sorted()完成自然排序、比較器和反向排序

          下面是sorted()方法的語法

          • sorted():它使用自然順序?qū)α髦械脑剡M(jìn)行排序。元素類必須實(shí)現(xiàn)Comparable接口。
          • sorted(Comparator comparator):這里我們使用lambda表達(dá)式創(chuàng)建一個(gè)Comparator實(shí)例。我們可以按升序和降序?qū)α髟剡M(jìn)行排序。

          下面的代碼行將按自然順序?qū)α斜磉M(jìn)行排序。

          list.stream().sorted()???

          要反轉(zhuǎn)自然順序,Comparator提供reverseOrder()方法。

          list.stream().sorted(Comparator.reverseOrder())

          下面的代碼行使用 Comparator 對(duì)列表進(jìn)行排序。

          list.stream().sorted(Comparator.comparing(Student::getAge))?

          為了顛倒順序,Comparator提供reversed()方法。

          list.stream().sorted(Comparator.comparing(Student::getAge).reversed())???

          在List中使用Stream sorted()方法

          下面我們對(duì)Student列表進(jìn)行排序操作。首先,我們將按自然順序排序,然后使用比較器(Comparator)。

          下面是顛倒自然排序和比較器提供的排序的例子。

          SortList.java

          package?com.concretepage;??
          import?java.util.ArrayList;??
          import?java.util.Comparator;??
          import?java.util.List;??
          import?java.util.stream.Collectors;??
          public?class?SortList?{??
          ?public?static?void?main(String[]?args)?{??
          ??List?list?=?new?ArrayList();??
          ??list.add(new?Student(1,?"Mahesh",?12));??
          ??list.add(new?Student(2,?"Suresh",?15));??
          ??list.add(new?Student(3,?"Nilesh",?10));??
          ????
          ??System.out.println("---Natural?Sorting?by?Name---");??
          ??List?slist?=?list.stream().sorted().collect(Collectors.toList());??
          ??slist.forEach(e?->?System.out.println("Id:"+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));??
          ????
          ??System.out.println("---Natural?Sorting?by?Name?in?reverse?order---");??
          ??slist?=?list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());??
          ??slist.forEach(e?->?System.out.println("Id:"+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));????
          ????
          ??System.out.println("---Sorting?using?Comparator?by?Age---");??
          ??slist?=?list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());??
          ??slist.forEach(e?->?System.out.println("Id:"+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));??
          ????
          ??System.out.println("---Sorting?using?Comparator?by?Age?with?reverse?order---");??
          ??slist?=?list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());??
          ??slist.forEach(e?->?System.out.println("Id:"+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));??
          ?}??
          }?

          Student.java

          package?com.concretepage;??

          public?class?Student?implements?Comparable<Student>?{??
          ?????private?int?id;??
          ?????private?String?name;??
          ?????private?int?age;??

          ?????public?Student(int?id,?String?name,?int?age)?{??
          ????????this.id?=?id;??
          ????????this.name?=?name;??
          ????????this.age?=?age;??
          ?????}??

          ?????public?int?getId()?{??
          ????????return?id;??
          ?????}??

          ?????public?String?getName()?{??
          ????????return?name;??
          ?????}??

          ?????public?int?getAge()?{??
          ????????return?age;??
          ?????}??

          ?????@Override??
          ?????public?int?compareTo(Student?ob)?{??
          ????????return?name.compareTo(ob.getName());??
          ?????}??

          ????@Override??
          ????public?boolean?equals(final?Object?obj)?{??
          ????????if?(obj?==?null)?{??
          ????????????return?false;??
          ????????}??

          ????????final?Student?std?=?(Student)?obj;??
          ????????if?(this?==?std)?{??
          ????????????return?true;??
          ????????}?else?{??
          ????????????return?(this.name.equals(std.name)?&&?(this.age?==?std.age));??
          ????????}??
          ????}??
          ????
          ????@Override??
          ????public?int?hashCode()?{??
          ????????int?hashno?=?7;??
          ????????hashno?=?13?*?hashno?+?(name?==?null???0?:?name.hashCode());??
          ????????return?hashno;??
          ????}???
          }??

          輸出

          ---Natural?Sorting?by?Name---  
          Id:1,?Name:?Mahesh,?Age:12
          Id:3,?Name:?Nilesh,?Age:10
          Id:2,?Name:?Suresh,?Age:15
          ---Natural?Sorting?by?Name?in?reverse?order---
          Id:2,?Name:?Suresh,?Age:15
          Id:3,?Name:?Nilesh,?Age:10
          Id:1,?Name:?Mahesh,?Age:12
          ---Sorting?using?Comparator?by?Age---
          Id:3,?Name:?Nilesh,?Age:10
          Id:1,?Name:?Mahesh,?Age:12
          Id:2,?Name:?Suresh,?Age:15
          ---Sorting?using?Comparator?by?Age?with?reverse?order---
          Id:2,?Name:?Suresh,?Age:15
          Id:1,?Name:?Mahesh,?Age:12
          Id:3,?Name:?Nilesh,?Age:10

          在Set中使用Stream sorted()方法

          下面我們對(duì)Student類的集合(Set)進(jìn)行排序操作,此類必須重寫equals()hashCode()方法來標(biāo)識(shí)唯一的元素。

          對(duì)于自然排序,學(xué)生類需要實(shí)現(xiàn)Comparable接口。

          在下面的例子中,我們將使用自然排序和比較器提供的排序?qū)线M(jìn)行排序。

          SortSet.java

          package?com.concretepage;

          import?java.util.Comparator;??
          import?java.util.HashSet;??
          import?java.util.Set;??

          public?class?SortSet?{??
          ????public?static?void?main(String[]?args)?{??
          ????????Set?set?=?new?HashSet();??
          ????????set.add(new?Student(1,?"Mahesh",?12));??
          ????????set.add(new?Student(2,?"Suresh",?15));??
          ????????set.add(new?Student(3,?"Nilesh",?10));??

          ????????System.out.println("---Natural?Sorting?by?Name---");??
          ????????set.stream().sorted().forEach(e?->?System.out.println("Id:"??
          ????????????????+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));??

          ????????System.out.println("---Natural?Sorting?by?Name?in?reverse?order---");??
          ????????set.stream().sorted(Comparator.reverseOrder()).forEach(e?->?System.out.println("Id:"??
          ????????????????+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));????

          ????????System.out.println("---Sorting?using?Comparator?by?Age---");??
          ????????set.stream().sorted(Comparator.comparing(Student::getAge))??
          ????????.forEach(e?->?System.out.println("Id:"+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));??

          ????????System.out.println("---Sorting?using?Comparator?by?Age?in?reverse?order---");??
          ????????set.stream().sorted(Comparator.comparing(Student::getAge).reversed())??
          ????????.forEach(e?->?System.out.println("Id:"+?e.getId()+",?Name:?"+e.getName()+",?Age:"+e.getAge()));??
          ????}??
          }???

          輸出

          ---Natural?Sorting?by?Name---  
          Id:1,?Name:?Mahesh,?Age:12
          Id:3,?Name:?Nilesh,?Age:10
          Id:2,?Name:?Suresh,?Age:15
          ---Natural?Sorting?by?Name?in?reverse?order---
          Id:2,?Name:?Suresh,?Age:15
          Id:3,?Name:?Nilesh,?Age:10
          Id:1,?Name:?Mahesh,?Age:12
          ---Sorting?using?Comparator?by?Age---
          Id:3,?Name:?Nilesh,?Age:10
          Id:1,?Name:?Mahesh,?Age:12
          Id:2,?Name:?Suresh,?Age:15
          ---Sorting?using?Comparator?by?Age?in?reverse?order---
          Id:2,?Name:?Suresh,?Age:15
          Id:1,?Name:?Mahesh,?Age:12
          Id:3,?Name:?Nilesh,?Age:10?

          在Map中使用Stream sorted()方法

          這里我們將按鍵和值對(duì)Map進(jìn)行排序。

          SortMap.java

          package?com.concretepage;?

          import?java.util.Comparator;??
          import?java.util.HashMap;??
          import?java.util.Map;??
          public?class?SortMap?{??
          ?public?static?void?main(String[]?args)?{??
          ??Map?map?=?new?HashMap<>();??
          ??map.put(15,?"Mahesh");??
          ??map.put(10,?"Suresh");??
          ??map.put(30,?"Nilesh");??
          ????
          ??System.out.println("---Sort?by?Map?Value---");??
          ?????????map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))??
          ???????????.forEach(e?->?System.out.println("Key:?"+?e.getKey()?+",?Value:?"+?e.getValue()));??
          ??
          ??System.out.println("---Sort?by?Map?Key---");??
          ?????????map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))??
          ???????????.forEach(e?->?System.out.println("Key:?"+?e.getKey()?+",?Value:?"+?e.getValue()));??
          ?}??
          }???

          輸出

          ---Sort?by?Map?Value---  
          Key:?15,?Value:?Mahesh
          Key:?30,?Value:?Nilesh
          Key:?10,?Value:?Suresh
          ---Sort?by?Map?Key---
          Key:?10,?Value:?Suresh
          Key:?15,?Value:?Mahesh
          Key:?30,?Value:?Nilesh?

          下面我們要對(duì)值為自定義對(duì)象的Map進(jìn)行排序。

          SortMapOfCustomObject.java

          package?com.concretepage;??

          import?java.util.Comparator;??
          import?java.util.HashMap;??
          import?java.util.Map;??
          public?class?SortMapOfCustomObject?{??
          ?public?static?void?main(String[]?args)?{??
          ??Map?map?=?new?HashMap<>();??
          ??map.put(1,?new?Student(1,?"Mahesh",?12));??
          ??map.put(2,?new?Student(2,?"Suresh",?15));??
          ??map.put(3,?new?Student(3,?"Nilesh",?10));??
          ????????????????//Map?Sorting?by?Value?i.e?student's?natural?ordering?i.e?by?name??
          ?????????map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))??
          ??????.forEach(e?->?{??
          ????Integer?key?=?(Integer)e.getKey();??
          ????Student?std?=?(Student)e.getValue();??
          ???????System.out.println("Key:?"?+?key?+",?value:?("+?std.getId()?+",?"+?std.getName()+",?"+?std.getAge()+")");???
          ??????});??
          ?}??
          }???

          輸出

          Key:?1,?value:?(1,?Mahesh,?12)??
          Key:?3,?value:?(3,?Nilesh,?10)??
          Key:?2,?value:?(2,?Suresh,?15)?

          瀏覽 19
          點(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>
                  激情性爱网站在线观看 | 二区无码 | 亚洲无码av在线播放 | 最新日韩黄色电影网站 | 中文无码乱伦特级毛片 |