• stream().sorted()以及java中常用的比较器


    1.sorted():无参数的情况下,需要对应的Bean实现Comparable 接口

    package com.itheima.demo18_扩展字符串排序;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Harbor_Stream {
        public static void main(String[] args) {
            List personList = new ArrayList();
            personList.add(new Person("Tom", 8900, 28,"male", "New York"));
            personList.add(new Person("Jack", 7000, 29,"male", "Washington"));
            personList.add(new Person("Lily", 7800, 320,"female", "Washington"));
            personList.add(new Person("Anni", 8200, 26,"female", "New York"));
            personList.add(new Person("Owen", 9500, 27,"male", "New York"));
            personList.add(new Person("Alisa", 7900, 29,"female", "New York"));
            personList.stream().sorted().forEach(s-> System.out.println(s));
        }
    }
    

     

    package com.itheima.demo18_扩展字符串排序;
    
    import java.util.Objects;
    
    public class Person implements Comparable{
        private String name; // 姓名
        private int salary; // 薪资
        private int age; // 年龄
        private String sex; //性别
        private String area; // 地区
    
        public String getName() {
            return name;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Person person = (Person) o;
            return salary == person.salary &&
                    age == person.age &&
                    Objects.equals(name, person.name) &&
                    Objects.equals(sex, person.sex) &&
                    Objects.equals(area, person.area);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name, salary, age, sex, area);
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", salary=" + salary +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    ", area='" + area + '\'' +
                    '}';
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getSalary() {
            return salary;
        }
    
        public void setSalary(int salary) {
            this.salary = salary;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public String getArea() {
            return area;
        }
    
        public void setArea(String area) {
            this.area = area;
        }
    
        // 构造方法
        public Person(String name, int salary, int age,String sex,String area) {
            this.name = name;
            this.salary = salary;
            this.age = age;
            this.sex = sex;
            this.area = area;
        }
    
    
        @Override
        public int compareTo(Object o) {
            Person good = (Person) o;
    
                return Integer.compare( this.getAge(),good.getAge());
    
        }
    }
    

     2.sorted():有参数的情况下直接按照stream流的传参格式进行传参

    package com.itheima.demo18_扩展字符串排序;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Harbor_Stream {
        public static void main(String[] args) {
            List personList = new ArrayList();
            personList.add(new Person("Tom", 8900, 28,"male", "New York"));
            personList.add(new Person("Jack", 7000, 29,"male", "Washington"));
            personList.add(new Person("Lily", 7800, 320,"female", "Washington"));
            personList.add(new Person("Anni", 8200, 26,"female", "New York"));
            personList.add(new Person("Owen", 9500, 27,"male", "New York"));
            personList.add(new Person("Alisa", 7900, 29,"female", "New York"));
            personList.stream().sorted(((o1, o2) ->{return o1.getAge()-o2.getAge();})).forEach(s-> System.out.println(s));
        }
    }
    

    3.

    Collections.sort(personList, new Comparator() 

     

    package com.itheima.demo18_扩展字符串排序;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Harbor_Stream {
        public static void main(String[] args) {
            List personList = new ArrayList();
            personList.add(new Person("Tom", 8900, 28,"male", "New York"));
            personList.add(new Person("Jack", 7000, 29,"male", "Washington"));
            personList.add(new Person("Lily", 7800, 320,"female", "Washington"));
            personList.add(new Person("Anni", 8200, 26,"female", "New York"));
            personList.add(new Person("Owen", 9500, 27,"male", "New York"));
            personList.add(new Person("Alisa", 7900, 29,"female", "New York"));
           // personList.stream().sorted(((o1, o2) ->{return o1.getAge()-o2.getAge();})).forEach(s-> System.out.println(s));
    
            Collections.sort(personList, new Comparator() {
                @Override
                public int compare(Person o1, Person o2) {
                    return o1.getAge()-o2.getAge();
                }
            });
            personList.forEach(s->{
                System.out.println(s);
            });
        }
    }
    

     

  • 相关阅读:
    动态规划篇——线性DP
    OpenCV(三十七):拟合直线、三角形和圆形
    进口气动不锈钢隔膜泵的选型说明
    c/c++4个内存分区介绍
    2022年全球及中国工程机械租赁行业头部企业市场占有率及排名调研报告
    【Linux】权限管理 _目录权限 _umask _粘滞位[学习总结 _复习专用]
    Docker Compose使用教程
    J2EE项目部署与发布(Windows版本)->会议OA单体项目Windows部署,spa前后端分离项目Windows部署
    Web Component-处理数据
    局域网远程yum源制做
  • 原文地址:https://blog.csdn.net/weixin_61503139/article/details/126081965