关于stream().sorted()以及java中常用的比较器排序
更新时间:2024年05月28日 10:51:10 作者: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);
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
详解Spring 框架中切入点 pointcut 表达式的常用写法
这篇文章主要介绍了详解Spring 框架中切入点 pointcut 表达式的常用写法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04如何使用eclipse搭建maven多module项目(构建父子项目)
这篇文章主要介绍了如何使用eclipse搭建maven多module项目(构建父子项目) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-12-12Spring线程池ThreadPoolExecutor配置并且得到任务执行的结果
今天小编就为大家分享一篇关于Spring线程池ThreadPoolExecutor配置并且得到任务执行的结果,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-03-03
最新评论