Map结果集排序

  /**
     * MAP结果集排序
     * @param sourceMap
     * @return
     */
    private Map<String, Object> sortData(Map<String, Object> sourceMap) {
        // 结果集
        Map<String, Object> resultMap = new HashMap<>();
        if (!sourceMap.isEmpty()) {
            List<Map.Entry<String, Object>> listData = new ArrayList<>(sourceMap.entrySet());
            // 排序
            Collections.sort(listData, (o1, o2) -> {
                //o1 to o2升序/o2 to o1降序
                return o2.getValue().toString().compareTo(o1.getValue().toString());
            });

            // 其它结果集
            int count = 0;
            for (int i = 0; i < listData.size(); i++) {
                Map.Entry<String, Object> entry = listData.get(i);
                // 取出排序前5
                if (i > 4) {
                    count += Integer.parseInt(entry.getValue().toString());
                } else {
                    resultMap.put(entry.getKey(), entry.getValue());
                }
            }
            resultMap.put("其它", count);
        }
        return resultMap;
    }

 

posted on 2018-12-14 10:45  HuaChenYing  阅读(417)  评论(0编辑  收藏  举报

导航