标签:userName 00 stream userNick age java8 Student 排序 createTime
效果
乱序
[
Student{userName='张三', userNick='2', age=22, createTime='2022-12-02 2:11:00'},
Student{userName='李四', userNick='1', age=23, createTime='2022-12-03 16:11:00'},
Student{userName='王五', userNick='4', age=24, createTime='2022-12-01 21:11:00'},
Student{userName='赵六', userNick='3', age=25, createTime='2022-12-02 22:11:00'}]
升序
[
Student{userName='王五', userNick='4', age=24, createTime='2022-12-01 21:11:00'},
Student{userName='赵六', userNick='3', age=25, createTime='2022-12-02 22:11:00'},
Student{userName='张三', userNick='2', age=22, createTime='2022-12-02 2:11:00'},
Student{userName='李四', userNick='1', age=23, createTime='2022-12-03 16:11:00'}]
降序[
Student{userName='李四', userNick='1', age=23, createTime='2022-12-03 16:11:00'},
Student{userName='张三', userNick='2', age=22, createTime='2022-12-02 2:11:00'},
Student{userName='赵六', userNick='3', age=25, createTime='2022-12-02 22:11:00'},
Student{userName='王五', userNick='4', age=24, createTime='2022-12-01 21:11:00'}]
代码实现
public class Test {
public static void main(String[] args) throws IOException {
//测试数据,请不要纠结数据的严谨性
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("张三","2",22,"2022-12-02 2:11:00"));
studentList.add(new Student("李四","1",23,"2022-12-03 16:11:00"));
studentList.add(new Student("王五","4",24,"2022-12-01 21:11:00"));
studentList.add(new Student("赵六","3",25,"2022-12-02 22:11:00"));
//根据日期进行升序排序
//乱序
System.out.println("乱序"+studentList);
//根据日期进行升序排序
List<Student> studentsSortAsce = studentList.stream().sorted(Comparator.comparing(Student::getCreateTime)).collect(Collectors.toList());
//根据日期进行降序排序
List<Student> studentsSortDesc = studentList.stream().sorted(Comparator.comparing(Student::getCreateTime).reversed()).collect(Collectors.toList());
//升序后输出
System.out.println("升序"+studentsSortAsce);
//降序后输出
System.out.println("降序"+studentsSortDesc);
}
}
class Student{
private String userName;
private String userNick;
private int age;
private String createTime;
public Student() {
}
public Student(String userName, String userNick, int age, String createTime) {
this.userName = userName;
this.userNick = userNick;
this.age = age;
this.createTime = createTime;
}
@Override
public String toString() {
return "Student{" +
"userName='" + userName + '\'' +
", userNick='" + userNick + '\'' +
", age=" + age +
", createTime='" + createTime + '\'' +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserNick() {
return userNick;
}
public void setUserNick(String userNick) {
this.userNick = userNick;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
reversed() 进行一个反转的操作
Student::此处填写要排序的字段
标签:userName,00,stream,userNick,age,java8,Student,排序,createTime
来源: https://www.cnblogs.com/rzkwz/p/16377861.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。