我是靠谱客的博主 不安小鸽子,这篇文章主要介绍Java8 stream 排序以及自定义比较器,很实用,现在分享给大家,希望可以做个参考。

 参考链接:

Java8 stream 排序以及自定义比较器,很实用!

猫巳的博客_CSDN博客-Java编程,Java 8,面试精选领域博主

在本文中,我们将提供 java 8 Stream sorted()排序的示例。我们可以按照自然顺序和比较器提供的顺序对流进行排序。

在Java8中,可以使用lambda表达式实例化比较器(Comparator)。

我们还可以颠倒自然顺序和比较器(Comparator)提供的顺序。

自然排序使用Comparable提供的排序,该排序必须由实例为流元素的类实现。

在本页中,我们将使用java 8 Stream sorted()方法对列表List, Map和Set进行排序。

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

下面是sorted()方法的语法

  • sorted():它使用自然顺序对流中的元素进行排序。元素类必须实现Comparable接口。

  • sorted(Comparator<? super T> comparator):这里我们使用lambda表达式创建一个Comparator实例。我们可以按升序和降序对流元素进行排序。

下面的代码行将按自然顺序对列表进行排序。

复制代码
1
2
list.stream().sorted() 

要反转自然顺序,Comparator提供reverseOrder()方法。

复制代码
1
2
list.stream().sorted(Comparator.reverseOrder()) 

下面的代码行使用Comparator对列表进行排序。

复制代码
1
2
list.stream().sorted(Comparator.comparing(Student::getAge)) 

为了颠倒顺序,Comparator提供reversed()方法。

复制代码
1
2
list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) 

2.在List中使用Stream sorted()方法

下面我们对Student列表进行排序操作。首先,我们将按自然顺序排序,然后使用比较器(Comparator)。

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

SortList.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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<Student> list = new ArrayList<Student>(); 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<Student> 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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;         }  } 

输出

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
---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 

3.在Set中使用Stream sorted()方法

下面我们对Student类的集合(Set)进行排序操作,此类必须重写equals()hashCode()方法来标识唯一的元素。

对于自然排序,学生类需要实现Comparable接口。

在下面的例子中,我们将使用自然排序和比较器提供的排序对集合进行排序。

SortSet.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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<Student> set = new HashSet<Student>();   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()));  } } 

输出

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
---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 

4.在Map中使用Stream sorted()方法

这里我们将按键和值对Map进行排序。

SortMap.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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<Integer, String> 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()));  } } 

输出

复制代码
1
2
3
4
5
6
7
8
9
---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 

下面我们要对值为自定义对象的Map进行排序。

SortMapOfCustomObject.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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<Integer, Student> 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()+")");        });  } } 

输出

复制代码
1
2
3
Key: 1, value: (1, Mahesh, 12) Key: 3, value: (3, Nilesh, 10) Key: 2, value: (2, Suresh, 15) 

最后

以上就是不安小鸽子最近收集整理的关于Java8 stream 排序以及自定义比较器,很实用的全部内容,更多相关Java8内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部