黑马程序员技术交流社区
标题:
【西安校区】Java集合List按日期升序或降序四种方法
[打印本页]
作者:
逆风TO
时间:
2019-11-5 15:51
标题:
【西安校区】Java集合List按日期升序或降序四种方法
四种方法快速预览
//Method 1、不管是Date、String、Long类型的日期都可以排序,无需转换
orders.sort((t1, t2) -> t2.getPayTime().compareTo(t1.getPayTime()));
//Method 2、利用stream,需要把字符串日期转换为时间戳
orders = orders.stream().sorted((t1,t2) ->
Long.compare(convertTimeToLong(t2.getPayTime()),
convertTimeToLong(t1.getPayTime()))).
collect(Collectors.toList());
//Method 3、利用Comparator接口实现,java8的写法
Collections.sort(orders, Comparator.comparing(Order::getPayTime, (t1, t2) -> t2.compareTo(t1)));
//Method 4、利用Comparator接口实现,java8之前通常这样写
Collections.sort(orders,new Comparator<Order>(){
@Override
public int compare(Order o1, Order o2) {
return o2.getPayTime().compareTo(o1.getPayTime());
}
});
准备的类
import lombok.AllArgsConstructor;
import lombok.Data;
@AllArgsConstructor
@Data
class Order {
/**
* 订单号
*/
private String orderNum;
/**
* 付款时间
*/
private String payTime;
/**
* 支付金额
*/
private Integer actualAmount;
}
方法一
import java.util.ArrayList;
import java.util.List;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//不管是Date、String、Long类型的日期都可以排序,无需转换
orders.sort((t1, t2) -> t2.getPayTime().compareTo(t1.getPayTime()));
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
}
方法二
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//利用stream,需要把字符串日期转换为时间戳
orders = orders.stream().sorted((t1,t2) ->
Long.compare(convertTimeToLong(t2.getPayTime()), convertTimeToLong(t1.getPayTime()))).
collect(Collectors.toList());
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
/**
* 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
*/
public static Long convertTimeToLong(String time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(time, formatter);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}
方法三
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//利用Comparator接口实现,java8的写法
Collections.sort(orders, Comparator.comparing(Order::getPayTime, (t1, t2) -> t2.compareTo(t1)));
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
}
方法四
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//利用Comparator接口实现,java8之前通常这样写
Collections.sort(orders,new Comparator<Order>(){
@Override
public int compare(Order o1, Order o2) {
return o2.getPayTime().compareTo(o1.getPayTime());
}
});
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
}
四种方法最后输出如下
------------排序前-----------------
orderNum=1102,payTime=2019-11-03 00:30:32
orderNum=1101,payTime=2019-11-03 00:30:31
orderNum=1104,payTime=2019-11-03 00:30:34
orderNum=1103,payTime=2019-11-03 00:30:33
orderNum=1105,payTime=2019-11-03 00:30:35
------------倒序后-----------------
orderNum=1105,payTime=2019-11-03 00:30:35
orderNum=1104,payTime=2019-11-03 00:30:34
orderNum=1103,payTime=2019-11-03 00:30:33
orderNum=1102,payTime=2019-11-03 00:30:32
orderNum=1101,payTime=2019-11-03 00:30:31
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2