当前位置:网站首页>对List集合中每个对象元素按时间顺序排序
对List集合中每个对象元素按时间顺序排序
2022-07-30 20:51:00 【Yield & Allure】
首先创建一个实体类
package com.huawei.Test;
import java.util.Date;
/**
* @author h84250472
* @title: User$
* @description: TODO
* @date 2022/6/29 10:54
*/
public class User {
private String name;
private String sex;
private Integer age;
private Date birthday;
public User(String name, String sex, Integer age, Date birthday) {
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
public User() {
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
之后通过把实体类加入到list集合中,并按照生日时间进行排序,此处用了两种方法进行排序,分别是stream和Collections进行排序,代码如下:
package com.huawei.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author h84250472
* @title: Test04$
* @description: TODO
* @date 2022/7/25 14:44
*/
public class Test04 {
public static void main(String[] args) throws ParseException {
List<User> list = new ArrayList<>();
long time1 = System.currentTimeMillis()+10000L;
long time2 = System.currentTimeMillis()+20000L;
long time3 = System.currentTimeMillis()+30000L;
long time4 = System.currentTimeMillis()+40000L;
//规定日期格式
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//将long类型的时间变成String类型
String format1 = sd.format(time1);
//将String类型的时间变成Date类型
Date parse1 = sd.parse(format1);
String format2 = sd.format(time2);
Date parse2 = sd.parse(format2);
String format3 = sd.format(time3);
Date parse3 = sd.parse(format3);
String format4 = sd.format(time4);
Date parse4 = sd.parse(format4);
User u1 = new User("zahngSan","nan",20,parse1);
User u2 = new User("lisi","nan",30,parse2);
User u3 = new User("wangwu","nan",26,parse3);
User u4 = new User("xiaofang","nv",20,parse4);
Collections.addAll(list,u1,u2,u3,u4);
// List<User> collect = list.stream().sorted(new Comparator<User>() {
// @Override
// public int compare(User o1, User o2) {
// Date date1 = o1.getBirthday();
// Date date2 = o2.getBirthday();
// return Long.compare(date2.getTime(), date1.getTime());
// }
// }).collect(Collectors.toList());
//用stream进行排序
List<User> collect = list.stream().sorted((User o1, User o2) -> {
Date date1 = o1.getBirthday();
Date date2 = o2.getBirthday();
return Long.compare(date2.getTime(), date1.getTime());
}
).collect(Collectors.toList());
for (User user : collect) {
System.out.println(user);
}
//用集合进行排序
Collections.sort(list, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
Date date1 = o1.getBirthday();
Date date2 = o2.getBirthday();
if (date1.getTime()>date2.getTime()){
return 1;
} else if (date1.getTime()<date2.getTime()){
return -1;
}else{
return 0;
}
}
});
}
}
边栏推荐
- MySQL的 DDL和DML和DQL的基本语法
- 多线程获取官方汇率
- js堆和栈
- [Typora] This beta version of Typora is expired, please download and install a newer version.
- Mysql——字符串函数
- 为什么那么多自学软件测试的人,后来都放弃了...
- 用于命名实体识别的模块化交互网络
- 微信读书,导出笔记
- Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]
- [Nuxt 3] (十四) Nuxt 生命周期
猜你喜欢
随机推荐
flyway的快速入门教程
我是一名阿里在职9年软件测试工程师,我的经历也许能帮到处于迷茫期的你
chrome extension: how to make the dialog be on the right side of the current window?
MySQL的主从复制
HJ85 longest palindrome substring
三层架构简单配置
excel数字如何转换成文本?excel表格数据转换成文本的方法
文字的选择与排版
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
明解C语言第五章习题
Based on the face of the common expression recognition - model building, training and testing
CDH集群spark-shell执行过程分析
vlan简单实验
【考研词汇训练营】Day18 —— amount,max,consider,account,actual,eliminate,letter,significant,embarrass,collapse
MySQL BIGINT 数据类型
为什么那么多自学软件测试的人,后来都放弃了...
2022年SQL经典面试题总结(带解析)
【Codeforces思维题】20220728
Mysql——字符串函数
mysqldump导出提示:mysqldump [Warning] Using a password on the command line interface can be insecure