Java8Stream流的sorted()排序===使用Comparator排序

2022-07-25,,,

强烈推荐大佬博客:https://blog.csdn.net/cdw_sunshine/article/details/90738726

                                https://blog.csdn.net/u014698430/article/details/52850046

                                https://blog.csdn.net/wjc_hbu/article/details/92853989

sorted()方法排序,一个是Comparable(自然排序),一个是Comparator接口,像Integer、String等这些基本类型的包装类已经实现了Comparable接口,

Comparable接口======》java.lang包下的

方法CompareTo(Object  o),除了基本类型包装类外,一些常用的pojo类要使用CompareTo(Object o)排序,就要实现Comparable接口,在pojo类里重写compareTo()方法

Comparator接口======》java.util包下的

方法compare(Object a,Object b)

关于Comparable和Comparator接口请看这位大神的博客

Sorted排序一共有两种,自然排序和Comparator接口的排序,

使用sorted()排序

  1. 简单List<Integer> 排序=====》
    1. package com.it.test;
      import java.util.ArrayList;
      
      import java.util.List;
      import java.util.stream.Collectors;
      
      public class StreamTest {
          public static void main(String[] args) {
              List<Integer> integers = new ArrayList<>();
              integers.add(9);
              integers.add(2);
              integers.add(0);
              integers.add(4);
              integers.add(8);
              List<Integer> collect = integers.stream().sorted().collect(Collectors.toList());  //自然排序
          }
      }

      结果

  2. 简单List<String>排序
    1. package com.it.test;
      import java.util.ArrayList;
      
      import java.util.List;
      import java.util.stream.Collectors;
      
      public class StreamTest {
          public static void main(String[] args) {
              List<String> strings = new ArrayList<>();
              strings.add("aa");
              strings.add("b");
              strings.add("aac");
              strings.add("bb");
              strings.add("abb");
              List<String> collect1 = strings.stream().sorted().collect(Collectors.toList());
              System.out.println(collect1.toString());
          }
      }

      结果

  3. 复杂实体对象pojo排序
    1. 使用Comparator排序
      1. Person.java
package com.it.pojo;

import java.util.Comparator;
import java.util.Objects;

@Data
@NoArgsConstructor
@ToString
public class Person {
    private String name;
    private Integer age;
}

 test.java===>先只根据年龄排序,后先根据年龄排序,再根据姓名排序,都升序

package com.it.test;
import java.util.ArrayList;

import java.util.List;
import com.it.pojo.Person;


import java.util.Comparator;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        Person person1 = new Person();
        person1.setAge(21);
        person1.setName("21");

        Person person2 = new Person();
        person2.setAge(19);
        person2.setName("19");

        Person person3 = new Person();
        person3.setAge(19);
        person3.setName("20");

        Person person4 = new Person();
        person4.setAge(20);
        person4.setName("20");

        Person person5 = new Person();
        person5.setAge(19);
        person5.setName("18");
        
        List<Person> people = new ArrayList<>();
        people.add(person1);
        people.add(person2);
        people.add(person3);
        people.add(person4);
        people.add(person5);
       
        List<Person> collect1 = people.stream().sorted((Comparator.comparing(Person::getAge))).collect(Collectors.toList());//只根据年龄排序,升序
        System.out.println(collect2);
        
         List<Person> collect2 = people.stream().sorted((Comparator.comparing(Person::getAge)).reversed()).collect(Collectors.toList());//只根据年龄排序,降序
        System.out.println(collect2);
        
        List<Person> collect3 = people.stream().sorted((Comparator.comparing(Person::getAge)).thenComparing(Comparator.comparing(Person::getName))).collect(Collectors.toList());//先根据年龄进行排序,遇见相同的,然后根据姓名进行排序,都升序
        System.out.println(collect3);

        
         List<Person> collect4 = people.stream().sorted((Comparator.comparing(Person::getAge)).thenComparing(Comparator.comparing(Person::getName)).reversed()).collect(Collectors.toList());/先根据年龄进行排序,遇见相同的,然后根据姓名进行排序,降序【都降序】
        System.out.println(collect4);

        
         
        List<Person> collect5 = people.stream().sorted(Comparator.comparing(Person::getAge).reversed().thenComparing(Comparator.comparing(Person::getName)).reversed()).collect(Collectors.toList());//年龄升序,姓名降序
        System.out.println(collect5);


        
       List<Person> collect6 = people.stream().sorted(Comparator.comparing(Person::getAge).reversed().thenComparing(Comparator.comparing(Person::getName))).collect(Collectors.toList());//根据年龄降序,姓名升序
        System.out.println(collect6);
     


    }
}

结果

只根据年龄进行排序的,默认是升序

根据年龄降序

     

 先根据年龄,后根据姓名排序的,默认都是升序

根据年龄和性别,先根据年龄,年龄相同根据性别,只在最后一个comparing上写reversed,两个排序都是降序【根据年龄降序,姓名降序】

根据年龄升序,年龄相同,根据姓名降序

 

总结

先这么着吧

本文地址:https://blog.csdn.net/dsl59741/article/details/112220414

《Java8Stream流的sorted()排序===使用Comparator排序.doc》

下载本文的Word格式文档,以方便收藏与打印。