对Map中的value进行排序

来源:互联网 发布:大数据 企业管理 编辑:程序博客网 时间:2024/05/17 23:41

对Map中的value进行排序,因为不同的值的比较方式不同,所以需要对该方法中的compare进行相应的调整,修改为value中对应的数据类型的比较方式,如果要调整升降序的话,只要修改compare方法中的最后返回的value的比较顺序的位置即可:


public static ArrayList<Map.Entry<Integer,BigDecimal>> sortMap(Map map){        List<Map.Entry<Integer, BigDecimal>> entries = new ArrayList<Map.Entry<Integer, BigDecimal>>(map.entrySet());        Collections.sort(entries, new Comparator<Map.Entry<Integer, BigDecimal>>() {            public int compare(Map.Entry<Integer, BigDecimal> obj1, Map.Entry<Integer, BigDecimal> obj2) {                return obj1.getValue().compareTo(obj2.getValue()) ;            }        });        return (ArrayList<Map.Entry<Integer, BigDecimal>>) entries;    }



原创粉丝点击