对实体类中的id和age进行排序:
☆第二个反序会对第一个的排序产生影响
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class test6 {
public static void main(String[] args) {
List<UserEntity> list = new ArrayList<>();
for (int i = 0; i < 50; i++) {
UserEntity entity = new UserEntity(
new Random().nextInt(3),
new Random().nextInt(5));
list.add(entity);
}
List<UserEntity> entityList = list.stream()
.sorted(Comparator.comparing(UserEntity::getId)
.thenComparing(UserEntity::getAge).reversed())
.limit(50).collect(Collectors.toList());
entityList.forEach(System.out::println);
}
}
class UserEntity{
private Integer id;
private Integer age;
private LocalDate birthday;
public UserEntity(Integer id, Integer age) {
this.id = id;
this.age = age;
}
public UserEntity(Integer id, Integer age, LocalDate birthday) {
this.id = id;
this.age = age;
this.birthday = birthday;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
结果如下:
UserEntity{id=2, age=4, birthday=null}
UserEntity{id=2, age=4, birthday=null}
UserEntity{id=2, age=4, birthday=null}
UserEntity{id=2, age=4, birthday=null}
UserEntity{id=2, age=3, birthday=null}
UserEntity{id=2, age=3, birthday=null}
UserEntity{id=2, age=3, birthday=null}
UserEntity{id=2, age=3, birthday=null}
UserEntity{id=2, age=2, birthday=null}
UserEntity{id=2, age=2, birthday=null}
UserEntity{id=2, age=2, birthday=null}
UserEntity{id=2, age=2, birthday=null}
UserEntity{id=2, age=1, birthday=null}
UserEntity{id=2, age=1, birthday=null}
UserEntity{id=2, age=1, birthday=null}
UserEntity{id=2, age=1, birthday=null}
UserEntity{id=1, age=4, birthday=null}
UserEntity{id=1, age=4, birthday=null}
UserEntity{id=1, age=4, birthday=null}
UserEntity{id=1, age=4, birthday=null}
UserEntity{id=1, age=4, birthday=null}
UserEntity{id=1, age=4, birthday=null}
UserEntity{id=1, age=3, birthday=null}
UserEntity{id=1, age=3, birthday=null}
UserEntity{id=1, age=2, birthday=null}
UserEntity{id=1, age=2, birthday=null}
UserEntity{id=1, age=2, birthday=null}
UserEntity{id=1, age=2, birthday=null}
UserEntity{id=1, age=2, birthday=null}
UserEntity{id=1, age=1, birthday=null}
UserEntity{id=1, age=1, birthday=null}
UserEntity{id=1, age=1, birthday=null}
UserEntity{id=1, age=1, birthday=null}
UserEntity{id=1, age=1, birthday=null}
UserEntity{id=1, age=0, birthday=null}
UserEntity{id=0, age=4, birthday=null}
UserEntity{id=0, age=4, birthday=null}
UserEntity{id=0, age=3, birthday=null}
UserEntity{id=0, age=3, birthday=null}
UserEntity{id=0, age=3, birthday=null}
UserEntity{id=0, age=3, birthday=null}
UserEntity{id=0, age=2, birthday=null}
UserEntity{id=0, age=2, birthday=null}
UserEntity{id=0, age=2, birthday=null}
UserEntity{id=0, age=2, birthday=null}
UserEntity{id=0, age=1, birthday=null}
UserEntity{id=0, age=0, birthday=null}
UserEntity{id=0, age=0, birthday=null}
UserEntity{id=0, age=0, birthday=null}
UserEntity{id=0, age=0, birthday=null}
对时间进行排序:
public static void main(String[] args) {
List<UserEntity> list = new ArrayList<>();
for (int i = 0; i < 50; i++) {
UserEntity entity = new UserEntity(
new Random().nextInt(3),
new Random().nextInt(5),
LocalDate.of(2021,8,new Random().nextInt(31)+1));
list.add(entity);
}
List<UserEntity> entityList = list.stream()
.sorted(Comparator.comparing(UserEntity::getAge)
.thenComparing(UserEntity::getBirthday))
.limit(50).collect(Collectors.toList());
entityList.forEach(System.out::println);
}
结果:
UserEntity{id=1, age=1, birthday=2021-08-06}
UserEntity{id=1, age=1, birthday=2021-08-06}
UserEntity{id=2, age=1, birthday=2021-08-07}
UserEntity{id=1, age=1, birthday=2021-08-13}
UserEntity{id=2, age=1, birthday=2021-08-15}
UserEntity{id=2, age=1, birthday=2021-08-19}
UserEntity{id=1, age=1, birthday=2021-08-22}
UserEntity{id=2, age=1, birthday=2021-08-28}
UserEntity{id=1, age=1, birthday=2021-08-30}
……