人生若只如初见

WXY

Java8利用stream流实现数字排序,中文排序,拼音排序

2022-07-25

利用stream的sorted()进行实现中文排序和数字排序(包括降序和升序),非常简单。

/**
 * @Description
 * @Author wxyShine
 * @Date 2022/7/25 15:41
 * @Email wxy@wxy97.com
 */
public class Main {


   static class Stu{
       private Integer id;
       private String name;
       private String grade;

       public Stu(Integer id, String name, String grade) {
           this.id = id;
           this.name = name;
           this.grade = grade;
       }

       public Integer getId() {
           return id;
       }

       public void setId(Integer id) {
           this.id = id;
       }

       public String getName() {
           return name;
       }

       public void setName(String name) {
           this.name = name;
       }

       public String getGrade() {
           return grade;
       }

       public void setGrade(String grade) {
           this.grade = grade;
       }

       @Override
       public String toString() {
           return "Stu{" +
                   "id=" + id +
                   ", name='" + name + '\'' +
                   ", grade='" + grade + '\'' +
                   '}';
       }
   }

    public static void main(String[] args) {
        //测试数据,可以看到顺序是乱的。
        List<Stu> user = new ArrayList<>();
        user.add( new Stu(1, "安静","yi"));
        user.add( new Stu(4, "李四","er"));
        user.add( new Stu(2, "王五","san"));
        user.add( new Stu(3, "赵六","si"));

        //按编号升序(默认)
        List<Stu> collect = user.stream().sorted(Comparator.comparingInt(Stu::getId)).collect(Collectors.toList());
        //遍历输出
        System.out.println("数字升序");
        collect.forEach(System.out::println);
        System.out.println("===========================================================================");

        //按姓名升序(默认)
        List<Stu> collect1 = user.stream()
                .sorted((s1, s2) -> Collator.getInstance(Locale.SIMPLIFIED_CHINESE).compare(s1.getName(),s2.getName())).collect(Collectors.toList());
        //遍历输出
        System.out.println("中文升序");
        collect1.forEach(System.out::println);
        System.out.println("===========================================================================");

        //按年级升序(默认)
        List<Stu> collect2 = user.stream()
                .sorted((s1, s2) -> Collator.getInstance(Locale.SIMPLIFIED_CHINESE).compare(s1.getGrade(),s2.getGrade())).collect(Collectors.toList());
        //遍历输出
        System.out.println("拼音升序");
        collect2.forEach(System.out::println);
        System.out.println("===========================================================================");


        //按编号降序
        List<Stu> collect3 = user.stream().sorted((o1, o2) -> o2.getId() - o1.getId()).collect(Collectors.toList());
        //遍历输出
        System.out.println("数字降序");
        collect3.forEach(System.out::println);
        System.out.println("===========================================================================");

        //按姓名降序
        List<Stu> collect4 =
                user.stream().sorted((s1, s2) -> Collator.getInstance(Locale.SIMPLIFIED_CHINESE).compare(s2.getName(),s1.getName())).collect(Collectors.toList());
        //遍历输出
        System.out.println("中文降序");
        collect4.forEach(System.out::println);

        //按年级降序
        List<Stu> collect5 =
                user.stream().sorted((s1, s2) -> Collator.getInstance(Locale.SIMPLIFIED_CHINESE).compare(s2.getGrade(),s1.getGrade())).collect(Collectors.toList());
        //遍历输出
        System.out.println("拼音降序");
        collect5.forEach(System.out::println);

    }

执行结果

数字升序
Stu{id=1, name='安静', grade='yi'}
Stu{id=2, name='王五', grade='san'}
Stu{id=3, name='赵六', grade='si'}
Stu{id=4, name='李四', grade='er'}
===========================================================================
中文升序
Stu{id=1, name='安静', grade='yi'}
Stu{id=4, name='李四', grade='er'}
Stu{id=2, name='王五', grade='san'}
Stu{id=3, name='赵六', grade='si'}
===========================================================================
拼音升序
Stu{id=4, name='李四', grade='er'}
Stu{id=2, name='王五', grade='san'}
Stu{id=3, name='赵六', grade='si'}
Stu{id=1, name='安静', grade='yi'}
===========================================================================
数字降序
Stu{id=4, name='李四', grade='er'}
Stu{id=3, name='赵六', grade='si'}
Stu{id=2, name='王五', grade='san'}
Stu{id=1, name='安静', grade='yi'}
===========================================================================
中文降序
Stu{id=3, name='赵六', grade='si'}
Stu{id=2, name='王五', grade='san'}
Stu{id=4, name='李四', grade='er'}
Stu{id=1, name='安静', grade='yi'}
拼音降序
Stu{id=1, name='安静', grade='yi'}
Stu{id=3, name='赵六', grade='si'}
Stu{id=2, name='王五', grade='san'}
Stu{id=4, name='李四', grade='er'}

Process finished with exit code 0