Stream中间操作之排序
这里我们先举一个例子
eg:
package stream流.中间操作.排序;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Demo1 {
public static void main(String[] args) {
List<Integer> arrayList = Arrays.asList(12,343,434,43);
arrayList.stream().sorted().forEach(System.out :: println);
}
}
这里我们再举一个例子
这个例子中涉及到了两个自定义类,这里先将这两个自定义类给出:
- Employee类
package stream流.中间操作;
import java.util.Objects;
public class Employee implements Comparable<Employee>{
private int bianHao;
private String name;
private int age;
private double salary;
public Employee(){
}
public Employee(int bianHao, String name, int age, double salary){
this.bianHao = bianHao;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getBianHao() {
return bianHao;
}
public void setBianHao(int bianHao) {
this.bianHao = bianHao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public int compareTo(Employee e){
int value = this.getAge() - e.getAge();
if(value == 0){
return Double.compare(this.getSalary(),e.getSalary());
}
return value;
}
@Override
public String toString() {
return "Employee{" +
"bianHao=" + bianHao +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return bianHao == employee.bianHao &&
age == employee.age &&
Double.compare(employee.salary, salary) == 0 &&
Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(bianHao, name, age, salary);
}
}
- 这个Employee类中我们实现了Comparable接口,并且重写了compareTo()方法
- 并且我们重写的compareTo()方法是根据多级排序的方式进行重写的
- 这里我们先进行了年龄上的比较,然后又进行了工资上的比较(也就是年龄如果不同就根据年龄进行排序,如果年龄想等,这时候我们就根据员工的工资进行排序)
- 并且我们重写的compareTo()方法是根据多级排序的方式进行重写的
- EmployeeDate类
package stream流.中间操作;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class EmployeeDate {
public EmployeeDate(){
}
public static List<Employee> getEmployeeDate(){
List<Employee> list = new ArrayList<>();
Employee e1 = new Employee(1001,"马化腾",20,8001);
Employee e2 = new Employee(1002,"马云",47,8701);
Employee e3 = new Employee(1003,"刘强东",34,8401);
Employee e4 = new Employee(1004,"曹操",32,4501);
Employee e5 = new Employee(1005,"刘备",12,5401);
Employee e6 = new Employee(1006,"张飞",21,6601);
list.add(e1);
list.add(e2);
list.add(e3);
list.add(e4);
list.add(e5);
list.add(e6);
return list;
}
}
这个例子中的集合中的元素的模板类是我们的用户自定义类,这个时候我们要去自己实现Comparable接口
package stream流.中间操作.排序;
import stream流.中间操作.Employee;
import stream流.中间操作.EmployeeDate;
import java.util.List;
public class Demo2 {
public static void main(String[] args) {
List<Employee> list = EmployeeDate.getEmployeeDate();
list.stream().sorted().forEach(System.out :: println);
}
}
- 根据我们传入的参数的排序方式进行排序(也就是定制排序)
- 这个根据我们的Stream流中的元素所在类中就可以不用重写Comparable接口
这个时候我们举一个例子:
这个时候也涉及到两个自定义类:
- Employee类
package stream流.中间操作;
import java.util.Objects;
public class Employee{
private int bianHao;
private String name;
private int age;
private double salary;
public Employee(){
}
public Employee(int bianHao, String name, int age, double salary){
this.bianHao = bianHao;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getBianHao() {
return bianHao;
}
public void setBianHao(int bianHao) {
this.bianHao = bianHao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"bianHao=" + bianHao +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return bianHao == employee.bianHao &&
age == employee.age &&
Double.compare(employee.salary, salary) == 0 &&
Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(bianHao, name, age, salary);
}
}
- EmployeeDate类
package stream流.中间操作;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class EmployeeDate {
public EmployeeDate(){
}
public static List<Employee> getEmployeeDate(){
List<Employee> list = new ArrayList<>();
Employee e1 = new Employee(1001,"马化腾",20,8001);
Employee e2 = new Employee(1002,"马云",47,8701);
Employee e3 = new Employee(1003,"刘强东",34,8401);
Employee e4 = new Employee(1004,"曹操",32,4501);
Employee e5 = new Employee(1005,"刘备",12,5401);
Employee e6 = new Employee(1006,"张飞",21,6601);
list.add(e1);
list.add(e2);
list.add(e3);
list.add(e4);
list.add(e5);
list.add(e6);
return list;
}
}
这个例子中我们使用sorted(Comparator c)方法,进行定制排序
package stream流.中间操作.排序;
import stream流.中间操作.Employee;
import stream流.中间操作.EmployeeDate;
import java.util.stream.Stream;
import java.util.List;
public class Demo3 {
public static void main(String[] args) {
List<Employee> list = EmployeeDate.getEmployeeDate();
Stream<Employee> stream = list.stream().sorted((e1,e2)->{
int value = Integer.compare(e1.getAge(),e2.getAge());
if(value == 0){
return Double.compare(e1.getSalary(),e2.getSalary());
}
return value;
});
stream.forEach(System.out :: println);
}
}
- 这个时候我们可以发现这例子中的Employee类中并没有实现Comparable接口,这个时候也可以进行排序,这个时候我们是通过参数中的Comparator的排序方式进行排序