List<Map> 多字段排序
程序员文章站
2022-04-15 19:05:53
package com.example.test;import com.beust.jcommander.internal.Lists;import com.beust.jcommander.internal.Maps;import com.example.utils.DateUtil;import org.junit.Test;import java.util.*;import java.util.stream.Collectors;/** * @author hujt49 * @...
package com.example.test;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.example.utils.DateUtil;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author
* @Description
* @create 2020-07-29 17:58
*/
public class CommonTest {
class PojoTest {
private int num;
private Date date;
public PojoTest(int num, Date date) {
this.num = num;
this.date = date;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "PojoTest{" +
"num=" + num +
", date=" + date +
'}';
}
}
@Test
public void test() {
List<PojoTest> pojos = Lists.newArrayList(
new PojoTest(10, DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),
new PojoTest(10, DateUtil.formatStringToDate("2020-07-29", "yyyy-MM-dd")),
new PojoTest(16, DateUtil.formatStringToDate("2020-07-28", "yyyy-MM-dd")),
new PojoTest(16, DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),
new PojoTest(20, DateUtil.formatStringToDate("2020-07-31", "yyyy-MM-dd"))
);
System.err.println("===================0======================");
// stream.sorted 类中单个字段排序
List<PojoTest> pojoTests = pojos.stream().sorted(Comparator.comparing(PojoTest::getDate)).collect(Collectors.toList());
pojoTests.forEach(System.err::println);
System.err.println("===================1======================");
// stream.sorted 类中单个字段排序
pojoTests = pojos.stream().sorted(Comparator.comparing(PojoTest::getNum)).collect(Collectors.toList());
pojoTests.forEach(System.err::println);
System.err.println("===================2======================");
// stream.sorted 类中多个字段排序
pojoTests = pojos.stream().sorted(Comparator.comparing(PojoTest::getNum).thenComparing(PojoTest::getDate, Comparator.reverseOrder())).collect(Collectors.toList());
pojoTests.forEach(System.err::println);
System.err.println("===================3======================");
// list.sort 类中多个字段排序
pojos.sort(Comparator.comparing(PojoTest::getNum).thenComparing(PojoTest::getDate, Comparator.reverseOrder()));
pojos.forEach(System.err::println);
System.err.println("===================4======================");
List<Map> maps = Lists.newArrayList(
Maps.newHashMap("aa", 10, "bb", DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),
Maps.newHashMap("aa", 10, "bb", DateUtil.formatStringToDate("2020-07-29", "yyyy-MM-dd")),
Maps.newHashMap("aa", 16, "bb", DateUtil.formatStringToDate("2020-07-28", "yyyy-MM-dd")),
Maps.newHashMap("aa", 16, "bb", DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),
Maps.newHashMap("aa", 20, "bb", DateUtil.formatStringToDate("2020-07-31", "yyyy-MM-dd"))
);
// List<Map> 使用sorted排序时,单个字段排序没有问题, 但是使用thenComparing排序多个字段就会出现问题,似乎是无法确认类型的问题,详细原因我也不是太清楚,有大佬知道的话麻烦可以在评论里说下,谢谢
maps = maps.stream().sorted(Comparator.comparing(a -> Long.parseLong(a.get("aa") + ""))).collect(Collectors.toList());
maps.forEach(System.err::println);
System.err.println("===================5======================");
// maps = maps.stream().sorted(Comparator.comparing(a -> Long.parseLong(a.get("aa") + "")).thenComparing(Comparator.comparing(a -> (Date) a.get("bb"), Comparator.reverseOrder()))).collect(Collectors.toList());
// 上面那种方式没法排序,但是直接使用list的sort排序却是可以的
Comparator<Map> aa = Comparator.comparing(a -> Long.parseLong(a.get("aa") + ""));
Comparator<Map> bb = Comparator.comparing(a -> (Date) a.get("bb"), Comparator.reverseOrder());
maps.sort(aa.thenComparing(bb));
maps.forEach(System.err::println);
}
}
本文地址:https://blog.csdn.net/xiaodao0706/article/details/107688389
推荐阅读
-
由Java中的Set,List,Map引出的排序技巧
-
C#List<T>排序多权重、自定义
-
Java中list里面存放map,根据map中的某一个字段进行排序
-
List<Map> 多字段排序
-
java8的新特性,Collections.sort(排序的List集合)的使用,对list封装Map里面的某个值进行排序
-
set list map set如何去重 map如何排序
-
利用apache Collections和google guava对list和map进行过滤和排序
-
Java中list里面存放map,根据map中的某一个字段进行排序
-
List<Map> 多字段排序
-
多用多学之Java中的Set,List,Map详解