当前位置:网站首页>对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;
}
}
});
}
}
边栏推荐
- 网络安全实验环境搭建
- Android Studio 实现登录注册-源代码 (连接MySql数据库)
- MySQL的DATE_FORMAT()函数将Date转为字符串
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
- MySQL数据库字段超长问题
- @Transactional注解在类上还是接口上使用,哪种方式更好?
- Structured Streaming报错记录:Overloaded method foreachBatch with alternatives
- Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
- Difference Between Concurrency and Parallelism
- 【考研词汇训练营】Day18 —— amount,max,consider,account,actual,eliminate,letter,significant,embarrass,collapse
猜你喜欢

想要写出好的测试用例,先要学会测试设计

Recommender systems: overview of the characteristics of architecture: user/item engineering -- -- -- -- -- -- -- -- > recall layer > sort layer - > test/evaluation 】 【 cold start problems, real-time 】

R package调试
![KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']](/img/24/762cec2b4495e80a3e45fac1fac13e.png)
KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']

为什么那么多自学软件测试的人,后来都放弃了...

Use the map function to operate on each element in the list It seems that you don't need a map

flowable工作流所有业务概念

ceph的部署练习

PR视频剪辑软件教程

化二次型为标准型
随机推荐
YOLO V3详解
Recommendation System - Sorting Layer: Sorting Layer Architecture [User and Item Feature Processing Steps]
mysql8安装步骤教程
【限时福利】21天学习挑战赛 - MySQL从入门到精通
无法正常访问服务器
HJ85 longest palindrome substring
深入浅出边缘云 | 3. 资源配置
vlookup函数匹配不出来的原因及解决方法
mysql死锁
7.联合索引(最左前缀原则)
Activiti 工作流引擎 详解
啊?现在初级测试招聘都要求会自动化了?
MYSQL 唯一约束
ENS 表情包域名火了!是炒作还是机遇?
chrome extension: how to make the dialog be on the right side of the current window?
mysql 递归函数with recursive的用法
Android studio连接MySQL并完成简单的登录注册功能
[Nuxt 3] (十四) Nuxt 生命周期
Mysql——字符串函数
Swift RegexBuilder Vs. Raku Grammar