list集合排序,java8新特性,学习⼀下
最近做⼀个项⽬,遇到了list<Entity>需要按时间倒叙排列发现Java8新特性封装的⽐较好:
例⼦:
List<Entity> list = wArrayList();
list.stream().sorted(Comparatorparing(Entity::getCreateTime).reversed()).List());
看到⼀篇博客总结的⽐较好(拷贝⼀下,留作纪念):
原⽂链接:
国外对Java8⼀系列总结的不错,翻译过来给⼤家共享
这篇⽂章将会讲解Java 8 Stream sorted()⽰例,我们能够以⾃然序或着⽤Comparator接⼝定义的排序规则来排序⼀个流。Comparator 能⽤⽤lambada表达式来初始化,我们还能够逆序⼀个已经排序的流。
接下来我们将会使⽤java 8 的流式 sorted 排序 List、Map、Set
1、sorted() 默认使⽤⾃然序排序,其中的元素必须实现Comparable 接⼝
2、sorted(Comparator<? super T> comparator):
我们可以使⽤lambada 来创建⼀个Comparator实例。可以按照升序或着降序来排序元素。
下⾯代码以⾃然序排序⼀个list
list.stream().sorted()
⾃然序逆序元素,使⽤Comparator提供的reverseOrder()⽅法
list.stream().verseOrder())
使⽤Comparator来排序⼀个list
list.stream().sorted(Comparatorparing(Student::getAge))
把上⾯的元素逆序
list.stream().sorted(Comparatorparing(Student::getAge).reversed())
Stream sorted() with List
我们排序⼀组装着Student类对象的List集合。⾸先我们使⽤⾃然序,接着我们使⽤Comparator分别进⾏升序和降序:SortList.java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortList {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "Mahesh", 12));
list.add(new Student(2, "Suresh", 15));
list.add(new Student(3, "Nilesh", 10));
System.out.println("---Natural Sorting by Name---");
List<Student> slist = list.stream().sorted().List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Natural Sorting by Name in reverse order---");
slist = list.stream().verseOrder()).List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Sorting using Comparator by Age---");
slist = list.stream().sorted(Comparatorparing(Student::getAge)).List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Sorting using Comparator by Age with reverse order---");
slist = list.stream().sorted(Comparatorparing(Student::getAge).reversed()).List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
}
}
* Student.java *
public class Student implements Comparable<Student> {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Student ob) {
return Name());
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
final Student std = (Student) obj;
if (this == std) {
return true;
} else {
return (this.name.equals(std.name) && (this.age == std.age));
}
}
@Override
public int hashCode() {
int hashno = 7;
hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
return hashno;
}
}
* Output *
---Natural Sorting by Name---
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Id:2, Name: Suresh, Age:15
---Natural Sorting by Name in reverse order---
Id:2, Name: Suresh, Age:15
Id:3, Name: Nilesh, Age:10java stream
Id:1, Name: Mahesh, Age:12
---Sorting using Comparator by Age---
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
Id:2, Name: Suresh, Age:15
---Sorting using Comparator by Age with reverse order---
Id:2, Name: Suresh, Age:15
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论