1.对于利用java8 stream 的sort
1.1对于sort的降序和升序操作
-
Comparator.comparing(类::属性一).reversed();
-
Comparator.comparing(类::属性一,Comparator.reverseOrder());
第一种是将结果先进行升序后再进行降序操作,第二种是直接进行降序操作。
所以在进行选择的时候,建议选择第二种。
1.2源码
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor)
{
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
default <U extends Comparable<? super U>> Comparator<T> thenComparing(
Function<? super T, ? extends U> keyExtractor)
{
return thenComparing(comparing(keyExtractor));
}
对于list的排序:
personList.stream().sorted(Comparator.comparing((Person p) -> -p.getCars().size()).thenComparing(Person::getId)).collect(Collectors.toList());
当然如果将comparing里面的代码分开来写,看的会更加清楚,特别指定了类型是Person。
Function<Person, Integer> function = p -> -p.getCars().size();
personList = personList.stream().sorted(Comparator.comparing(function).thenComparing(Person::getId)).collect(Collectors.toList());
完整代码如下 :
public class Car {
private String carName;
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
@Override
public String toString() {
return "Car{" +
"carName='" + carName + '\'' +
'}';
}
}
public class Person {
private int id;
private String name;
private List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
Person() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Person p1 = new Person(9, "aa");
p1.setCars(new ArrayList<>());
Car car10 = new Car();
car10.setCarName("car10");
Car car11 = new Car();
car11.setCarName("car11");
p1.getCars().add(car10);
p1.getCars().add(car11);
Person p2 = new Person(3, "aa");
p2.setCars(new ArrayList<>());
Car car20 = new Car();
car20.setCarName("car20");
Car car21 = new Car();
car21.setCarName("car21");
Car car23 = new Car();
car23.setCarName("car23");
p2.getCars().add(car20);
p2.getCars().add(car21);
p2.getCars().add(car23);
List<Person> personList = new ArrayList<>();
personList.add(p1);
personList.add(p2);
Function<Person, Integer> function = p -> -p.getCars().size();
personList = personList.stream().sorted(Comparator.comparing(function).thenComparing(Person::getId)).collect(Collectors.toList());
System.out.println(personList);
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", cars=" + cars +
'}';
}
}
1.3进行包含null字段的排序
comparable包含对null判断的源码:
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor)
{
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
Objects.requireNonNull(keyExtractor);这个方法里面要求被比较属性不能为null,否则报错。
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
endList.stream().sorted(Comparator.comparing(AjsAttend::getParea,Comparator.nullsFirst(String::compareTo)).reversed()
.thenComparing(AjsAttend::getClassId,Comparator.nullsFirst(String::compareTo)).reversed()
.thenComparing(AjsAttend::getShiftDate,Comparator.nullsFirst(LocalDate::compareTo)).reversed()
.thenComparing(AjsAttend::getCardTime,Comparator.nullsFirst(LocalDateTime::compareTo)))
.collect(Collectors.toList());
End 总结:
进行使用sort排序条件,清洗符合自己的数据格式。在进行排序的时候,要注意内部排序所花费的时间效率。