關于stream().sorted()以及java中常用的比較器排序
更新時間:2024年05月28日 10:51:10 作者:Harbor Lau
這篇文章主要介紹了關于stream().sorted()以及java中常用的比較器排序,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
1.sorted():無參數(shù)的情況下
需要對應的Bean實現(xiàn)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; // 地區(qū)
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():有參數(shù)的情況下
直接按照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);

總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
微服務如何通過feign.RequestInterceptor傳遞參數(shù)
這篇文章主要介紹了微服務如何通過feign.RequestInterceptor傳遞參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot 使用Spring Boot Actuator監(jiān)控應用小結
本篇文章主要介紹了springboot 使用Spring Boot Actuator監(jiān)控應用小結,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
IDEA生成項目后出現(xiàn)的iml和idea文件問題
這篇文章主要介紹了IDEA生成項目后出現(xiàn)的iml和idea文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

