对List<Map>数据排序

简介: 原始数据排序规则MapCompare类具体代码多重条件排序时排序后结果


对List< Map >数据排序

原始数据

{"msg":"成功","code":0,"obj":[{"importance":3,"childList":[{"importance":3,"kpId":2091,"kpName":"增值税应纳税额的计算-一般计税方法应纳税额的计算","attrStatus":2,"kpScore":1.00,"isExamPoint":1,"baseKpId":2139,"isMust":1},{"importance":3,"kpId":2093,"kpName":"增值税应纳税额的计算-简易计税方法应纳税额的计算","attrStatus":2,"kpScore":1.00,"isExamPoint":1,"baseKpId":2141,"isMust":1},{"importance":3,"kpId":2095,"kpName":"增值税应纳税额的计算-进口货物应纳税额的计算","attrStatus":2,"kpScore":0.67,"isExamPoint":1,"baseKpId":2143,"isMust":1}],"kpName":"增值税应纳税额的计算","kpId":2089,"attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2137,"isMust":1},{"importance":3,"childList":[{"importance":3,"kpId":2063,"kpName":"增值税征税范围-销售货物","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2111,"isMust":1},{"importance":3,"kpId":2065,"kpName":"增值税征税范围-销售劳务","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2113,"isMust":1},{"importance":3,"kpId":2067,"kpName":"增值税征税范围-销售服务","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2115,"isMust":1},{"importance":3,"kpId":2069,"kpName":"增值税征税范围-销售无形资产","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2117,"isMust":1},{"importance":3,"kpId":2071,"kpName":"增值税征税范围-销售不动产","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2119,"isMust":1},{"importance":3,"kpId":2073,"kpName":"增值税征税范围-进口货物","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2121,"isMust":1},{"importance":3,"kpId":2075,"kpName":"增值税征税范围-非经营活动的界定","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2123,"isMust":1},{"importance":3,"kpId":2077,"kpName":"增值税征税范围-境内销售服务、无形资产或者不动产的界定","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2125,"isMust":1},{"importance":3,"kpId":2079,"kpName":"增值税征税范围-视同销售货物行为","attrStatus":2,"kpScore":0.19,"isExamPoint":1,"baseKpId":2127,"isMust":1},{"importance":3,"kpId":2081,"kpName":"增值税征税范围-混合销售","attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2129,"isMust":1},{"importance":3,"kpId":2083,"kpName":"增值税征税范围-兼营","attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2131,"isMust":1},{"importance":3,"kpId":2085,"kpName":"增值税征税范围-不征收增值税项目","attrStatus":2,"kpScore":0.19,"isExamPoint":1,"baseKpId":2133,"isMust":1}],"kpName":"增值税征税范围","kpId":2061,"attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2109,"isMust":1},{"importance":3,"childList":[{"importance":3,"kpId":2113,"kpName":"消费税应纳税额的计算-销售额的确定","attrStatus":2,"kpScore":0.50,"isExamPoint":1,"baseKpId":2161,"isMust":1},{"importance":3,"kpId":2115,"kpName":"消费税应纳税额的计算-应纳税额的计算","attrStatus":2,"kpScore":0.50,"isExamPoint":1,"baseKpId":2163,"isMust":1},{"importance":3,"kpId":2117,"kpName":"消费税应纳税额的计算-已纳消费税的扣除","attrStatus":2,"kpScore":0.49,"isExamPoint":1,"baseKpId":2165,"isMust":1}],"kpName":"消费税应纳税额的计算","kpId":2111,"attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2159,"isMust":1},{"importance":1,"childList":[],"kpName":"税收与税收法律关系","kpId":2053,"attrStatus":2,"isExamPoint":1,"baseKpId":2101,"isMust":0},{"importance":1,"childList":[],"kpName":"税法要素","kpId":2055,"attrStatus":2,"isExamPoint":1,"baseKpId":2103,"isMust":1},{"importance":1,"childList":[],"kpName":"现行税种与征收机关","kpId":2057,"attrStatus":2,"isExamPoint":1,"baseKpId":2105,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税纳税人和扣缴义务人","kpId":2059,"attrStatus":2,"isExamPoint":1,"baseKpId":2107,"isMust":1},{"importance":1,"childList":[],"kpName":"增值税税率和征收率","kpId":2087,"attrStatus":2,"isExamPoint":1,"baseKpId":2135,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税税收优惠","kpId":2097,"attrStatus":2,"isExamPoint":1,"baseKpId":2145,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税征收管理","kpId":2099,"attrStatus":2,"isExamPoint":1,"baseKpId":2147,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税专用发票使用规定","kpId":2101,"attrStatus":2,"isExamPoint":1,"baseKpId":2149,"isMust":1},{"importance":2,"childList":[],"kpName":"消费税纳税人","kpId":2103,"attrStatus":2,"isExamPoint":1,"baseKpId":2151,"isMust":1},{"importance":3,"childList":[],"kpName":"消费税征税范围","kpId":2105,"attrStatus":2,"isExamPoint":1,"baseKpId":2153,"isMust":1},{"importance":2,"childList":[],"kpName":"消费税税目","kpId":2107,"attrStatus":2,"isExamPoint":1,"baseKpId":2155,"isMust":1},{"importance":1,"childList":[],"kpName":"消费税税率","kpId":2109,"attrStatus":2,"isExamPoint":1,"baseKpId":2157,"isMust":1},{"importance":2,"childList":[],"kpName":"消费税征收管理","kpId":2119,"attrStatus":2,"isExamPoint":1,"baseKpId":2167,"isMust":1},{"importance":1,"childList":[],"kpName":"第四章小结","kpId":2121,"attrStatus":2,"isExamPoint":0,"baseKpId":2169,"isMust":0}]}


排序规则

按kpScore倒序排列

list.stream().sorted(Comparator.comparing(MapCompare::comparingByKpScore).reversed())
                        .collect(Collectors.toList());


MapCompare类

public class MapCompare {
    public static BigDecimal comparingByKpScore(Map map){
        return (BigDecimal) map.get("kpScore");
    }
//、、、、、、如果需要其他字段排序可在下方补充
}


具体代码

public List<Map> getKpListByChapterIdOrder(String chapterId) {
    //获取原始数据
        List<Map> resuList = getKpListByChapterId(chapterId.toString());
        //对原始数据字段进行整理
        //返回值list
        List<Map> chapterKpIds = new ArrayList<>();
        Map resuMap;
        //遍历取子级知识点
        for (Map map : resuList) {
            //判断是否有子级
            List<Map> childList = (List<Map>) map.get("childList");
            if (CollectionUtils.isNotEmpty(childList)) {
                //有子级知识点,则取子级知识点
                for (Map cm : childList) {
                    resuMap = new HashMap(8);
                    resuMap.put("kpId",cm.get("kpId").toString());
                    resuMap.put("kpName",cm.get("kpName"));
                    resuMap.put("kpScore",cm.get("kpScore"));
                    chapterKpIds.add(resuMap);
                }
            }else {
                //没有子级则取父级
                resuMap = new HashMap(8);
                resuMap.put("kpId",map.get("kpId").toString());
                resuMap.put("kpName",map.get("kpName"));
                resuMap.put("kpScore",map.get("kpScore"));
                chapterKpIds.add(resuMap);
            }
        }
        //按kpScore倒序排列
        chapterKpIds =
                chapterKpIds.stream().sorted(Comparator.comparing(MapCompare::comparingByKpScore).reversed())
                        .collect(Collectors.toList());
        //返回数据
        return chapterKpIds;
    }


多重条件排序时

list.stream().sorted(Comparator.comparing(MapCompare::comparingByKpScore).reversed()
        .thenComparing(Comparator.comparing(MapCompare::comparingByOther)))
                        .collect(Collectors.toList());

排序后结果

[{"kpScore":1.20,"kpName":"消费税征税范围","kpId":"2105"},{"kpScore":1.15,"kpName":"增值税专用发票使用规定","kpId":"2101"},{"kpScore":1.05,"kpName":"增值税税收优惠","kpId":"2097"},{"kpScore":1.00,"kpName":"增值税应纳税额的计算-一般计税方法应纳税额的计算","kpId":"2091"},{"kpScore":1.00,"kpName":"增值税应纳税额的计算-简易计税方法应纳税额的计算","kpId":"2093"},{"kpScore":0.67,"kpName":"增值税应纳税额的计算-进口货物应纳税额的计算","kpId":"2095"},{"kpScore":0.67,"kpName":"消费税税率","kpId":"2109"},{"kpScore":0.62,"kpName":"现行税种与征收机关","kpId":"2057"},{"kpScore":0.62,"kpName":"增值税征收管理","kpId":"2099"},{"kpScore":0.52,"kpName":"消费税税目","kpId":"2107"},{"kpScore":0.50,"kpName":"消费税应纳税额的计算-销售额的确定","kpId":"2113"},{"kpScore":0.50,"kpName":"消费税应纳税额的计算-应纳税额的计算","kpId":"2115"},{"kpScore":0.49,"kpName":"消费税应纳税额的计算-已纳消费税的扣除","kpId":"2117"},{"kpScore":0.43,"kpName":"增值税纳税人和扣缴义务人","kpId":"2059"},{"kpScore":0.43,"kpName":"消费税征收管理","kpId":"2119"},{"kpScore":0.20,"kpName":"增值税征税范围-销售货物","kpId":"2063"},{"kpScore":0.20,"kpName":"增值税征税范围-销售劳务","kpId":"2065"},{"kpScore":0.20,"kpName":"增值税征税范围-销售服务","kpId":"2067"},{"kpScore":0.20,"kpName":"增值税征税范围-销售无形资产","kpId":"2069"},{"kpScore":0.20,"kpName":"增值税征税范围-销售不动产","kpId":"2071"},{"kpScore":0.20,"kpName":"增值税征税范围-进口货物","kpId":"2073"},{"kpScore":0.20,"kpName":"增值税征税范围-非经营活动的界定","kpId":"2075"},{"kpScore":0.20,"kpName":"增值税征税范围-境内销售服务、无形资产或者不动产的界定","kpId":"2077"},{"kpScore":0.19,"kpName":"增值税征税范围-视同销售货物行为","kpId":"2079"},{"kpScore":0.19,"kpName":"增值税征税范围-不征收增值税项目","kpId":"2085"},{"kpScore":0.19,"kpName":"税收与税收法律关系","kpId":"2053"},{"kpScore":0.19,"kpName":"增值税税率和征收率","kpId":"2087"},{"kpScore":0.00,"kpName":"增值税征税范围-混合销售","kpId":"2081"},{"kpScore":0.00,"kpName":"增值税征税范围-兼营","kpId":"2083"},{"kpScore":0.00,"kpName":"税法要素","kpId":"2055"},{"kpScore":0.00,"kpName":"消费税纳税人","kpId":"2103"},{"kpScore":0.00,"kpName":"第四章小结","kpId":"2121"}]


相关文章
|
20天前
|
存储 Java API
深入剖析Java Map:不只是存储数据,更是设计艺术的体现!
【10月更文挑战第17天】在Java编程中,Map是一种重要的数据结构,用于存储键值对,并展现了设计艺术的精髓。本文深入剖析了Map的设计原理和使用技巧,包括基本概念、设计艺术(如哈希表与红黑树的空间时间权衡)、以及使用技巧(如选择合适的实现类、避免空指针异常等),帮助读者更好地理解和应用Map。
60 3
SpringMVC入门到实战------5、域对象共享数据 Request、Session、Application、Model、ModelAndView、Map、ModelMap的详细使用及代码实例
这篇文章详细解释了在IntelliJ IDEA中如何使用Mute Breakpoints功能来快速跳过程序中的后续断点,并展示了如何一键清空所有设置的断点。
SpringMVC入门到实战------5、域对象共享数据 Request、Session、Application、Model、ModelAndView、Map、ModelMap的详细使用及代码实例
|
2月前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
35 5
|
2月前
|
NoSQL Java Redis
List集合按照由小到大排序或者由大到小排序
List集合按照由小到大排序或者由大到小排序
20 3
|
1月前
|
前端开发 JavaScript 索引
JavaScript 数组常用高阶函数总结,包括插入,删除,更新,反转,排序等,如map、splice等
JavaScript数组的常用高阶函数,包括遍历、插入、删除、更新、反转和排序等操作,如map、splice、push、pop、reverse等。
17 0
域对象共享数据model、modelAndView、map、mapModel、request。从源码角度分析
这篇文章详细解释了在IntelliJ IDEA中如何使用Mute Breakpoints功能来快速跳过程序中的后续断点,并展示了如何一键清空所有设置的断点。
域对象共享数据model、modelAndView、map、mapModel、request。从源码角度分析
|
3月前
|
存储 安全 Java
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set
|
2月前
|
Python
Python量化炒股的获取数据函数— get_billboard_list()
Python量化炒股的获取数据函数— get_billboard_list()
|
2月前
|
Java API 开发者
代码小妙招:用Java轻松获取List交集数据
在Java中获取两个 `List`的交集可以通过 `retainAll`方法和Java 8引入的流操作来实现。使用 `retainAll`方法更为直接,但会修改原始 `List`的内容。而使用流则提供了不修改原始 `List`、更为灵活的处理方式。开发者可以根据具体的需求和场景,选择最适合的方法来实现。了解和掌握这些方法,能够帮助开发者在实际开发中更高效地处理集合相关的问题。
91 1
|
4月前
|
Java API 存储
Java如何对List进行排序?
【7月更文挑战第26天】
198 9
Java如何对List进行排序?