List<Map>集合如何使用Stream流进行排序

起因:最近在工作中遇到一个问题,我从数据库中查询数据,返回的数据是一个List集合,且Map未指定类型。然后需要根据Map中的某个值对数组进行排序。虽然利用for循环可以完成这个工作,但是感觉代码太复杂,不美观而且不符合我们jdk1.8的特性。随决定使用Stream流进行排序。

1、我要查询根据月份查询数据,这是我的最开始返回的结果,可以看见第12月排在第一条了,我理想的数据是从1月开始。

[
    {
        "lastYear": [
            {
                "monthtime": "12",
                "operationtime": 11033
            },
            {
                "monthtime": "01",
                "operationtime": 0
            },
            {
                "monthtime": "02",
                "operationtime": 0
            },
            {
                "monthtime": "03",
                "operationtime": 0
            },
            {
                "monthtime": "04",
                "operationtime": 0
            },
            {
                "monthtime": "05",
                "operationtime": 0
            },
            {
                "monthtime": "06",
                "operationtime": 0
            },
            {
                "monthtime": "07",
                "operationtime": 0
            },
            {
                "monthtime": "08",
                "operationtime": 0
            },
            {
                "monthtime": "09",
                "operationtime": 0
            },
            {
                "monthtime": "10",
                "operationtime": 0
            },
            {
                "monthtime": "11",
                "operationtime": 0
            },
        ]
    }
]

然后我就想着对该数据进行排序处理。因为流排序的效率非常高。
**

这是我自己编写的排序代码:

**

list.stream().sorted(Comparator.comparing(map -> Integer.parseInt(map.get("monthtime").toString()))).collect(Collectors.toList());

看一下Comparator.comparing的源码:

 * @param  <T> the type of element to be compared
 * @param  <U> the type of the {@code Comparable} sort key
 * @param  keyExtractor the function used to extract the {@link
 *         Comparable} sort key
 * @return a comparator that compares by an extracted key
 * @throws NullPointerException if the argument is null
 * @since 1.8
 */
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor)
{
    Objects.requireNonNull(keyExtractor);
    return (Comparator<T> & Serializable)
        (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}

这个是函数式编程的特点,参数是一个方法,利用这个方法我们可以告诉排序方法我们需要根据什么来进行排序。我这里需要根据monthtime键对应的值进行排序,利用lambda表达式,把该函数传入即可。

排序后的结果:

[
    {
        "lastYear": [
            {
                "monthtime": "01",
                "operationtime": 0
            },
            {
                "monthtime": "02",
                "operationtime": 0
            },
            {
                "monthtime": "03",
                "operationtime": 0
            },
            {
                "monthtime": "04",
                "operationtime": 0
            },
            {
                "monthtime": "05",
                "operationtime": 0
            },
            {
                "monthtime": "06",
                "operationtime": 0
            },
            {
                "monthtime": "07",
                "operationtime": 0
            },
            {
                "monthtime": "08",
                "operationtime": 0
            },
            {
                "monthtime": "09",
                "operationtime": 0
            },
            {
                "monthtime": "10",
                "operationtime": 0
            },
            {
                "monthtime": "11",
                "operationtime": 0
            },
            {
                "monthtime": "12",
                "operationtime": 11033
            }
        ]
    }
]

如有问题,请指正,谢谢!

posted @ 2021-06-06 12:11  昨夜风雨声  阅读(192)  评论(0编辑  收藏  举报  来源