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);
});
}
}