当前位置:网站首页>对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;
}
}
});
}
}
边栏推荐
猜你喜欢
excel数字显示e+17怎么恢复?excel数字变成了小数点+E+17的解决方法
365天挑战LeetCode1000题——Day 044 按公因数计算最大组件大小 并查集
Zabbix部署与练习
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
c语言:操作符详解
vookloop函数怎么用?vlookup函数的使用方法介绍
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]
什么是公约数
CDH集群spark-shell执行过程分析
第03章 用户和权限管理【1.MySQL架构篇】【MySQL高级】
随机推荐
MySQL的DATE_FORMAT()函数将Date转为字符串
TensorFlow2: Overview
excel数字下拉递增怎么设置?
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
MySQL的on duplicate key update 的使用
Multi-threaded mutex application RAII mechanism
MySQL的Replace用法详解
Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
vlookup函数匹配不出来只显示公式的解决方法
MySQL60题作业
为什么那么多自学软件测试的人,后来都放弃了...
2021年PHP-Laravel面试题问卷题 答案记录
5分钟搞懂MySQL - 行转列
[The Beauty of Software Engineering - Column Notes] 31 | Is software testing responsible for product quality?
转义字符笔记记录
【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
Difference Between Concurrency and Parallelism
外包干了三年,废了...
WPS表格怎么自动1234排下去?wps表格怎么自动生成序号?