java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > stream().sorted()及java常用比较器排序

关于stream().sorted()以及java中常用的比较器排序

作者:Harbor Lau

这篇文章主要介绍了关于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<Person> personList = new ArrayList<Person>();
        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<Person> personList = new ArrayList<Person>();
        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<Person>()

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<Person> personList = new ArrayList<Person>();
        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<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge()-o2.getAge();
            }
        });
        personList.forEach(s->{
            System.out.println(s);
        });
    }
}

4.还是sort进行还需的写法

采用表达式

list.add(new Student("张三",16));
  list.add(new Student("张四",19));
  list.add(new Student("李五",16));
  list.add(new Student("张马六",16));
/*  Collections.sort(list, new Comparator<Student>() {
      @Override
      public int compare(Student o1, Student o2) {
          return o2.getName().compareTo(o1.getName());
      }
  });*/
  List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getAge).reversed().thenComparing(Student::getName))
          .collect(Collectors.toList());
  System.out.println(collect);
  // System.out.println(list);

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文