欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

相关标签: Tips java