首页 > 其他 > 详细

对Map中数据,按value值排序方法

时间:2014-04-10 12:00:16      阅读:720      评论:0      收藏:0      [点我收藏+]

1 Map类型


                //声明
        Map hashMap = new HashMap();
        //向Map中添加数据
        //.....
        //转换
        ArrayList> arrayList = new ArrayList>(
                    hashMap.entrySet());
        //排序
        Collections.sort(arrayList, new Comparator>() {
            public int compare(Map.Entry map1,
                        Map.Entry map2) {
                return (map2.getValue() - map1.getValue());
            }
        });
        //输出
        for (Entry entry : arrayList) {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }

2 Map类型


                //声明
        Map hashMap = new HashMap();
        //向Map中添加数据
        //.....
        //转换
        ArrayList> arrayList = new ArrayList>(hashMap.entrySet());
        //排序
        Collections.sort(arrayList, new Comparator>(){
            public int compare(Map.Entry map1,
                    Map.Entry map2) {
                return ((map2.getValue() - map1.getValue() == 0) ? 0
                        : (map2.getValue() - map1.getValue() > 0) ? 1
                                : -1);
            }
        });
        //输出
        for (Entry entry : arrayList) {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }

3 Map类型


                //声明
        Map hashMap = new HashMap();
        //向Map中添加数据
        //.....
        //转换
        ArrayList> arrayList = new ArrayList>(hashMap.entrySet());
        //排序
        Collections.sort(arrayList, new Comparator>(){
            public int compare(Map.Entry map1,
                    Map.Entry map2) {
                return ((map2.getValue() - map1.getValue() == 0) ? 0
                        : (map2.getValue() - map1.getValue() > 0) ? 1
                                : -1);
            }
        });
        //输出
        for (Entry entry : arrayList) {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }

对Map中数据,按value值排序方法,布布扣,bubuko.com

对Map中数据,按value值排序方法

原文:http://www.cnblogs.com/letters-zhang/p/3655109.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!