`
y8820960
  • 浏览: 112955 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Map的value排序

    博客分类:
  • j2se
 
阅读更多
package com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class SortMap
{
    public static void main(String[] args)
    {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("c", 3);
        map.put("b", 2);
        map.put("d", 4);
        map.put("f", 6);
        map.put("e", 5);
        ArrayList<Map.Entry<String, Integer>> list = sortMap(map);
        for(int i=0; i<list.size(); i++)
        {
            System.out.println(list.get(i).getValue());
        }
    }
    
    public static ArrayList<Map.Entry<String, Integer>> sortMap(Map map)
    {
        List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(
                map.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare(Map.Entry<String, Integer> obj1,
                    Map.Entry<String, Integer> obj2)
            {
                return obj1.getValue() - obj2.getValue();
            }
        });
        return (ArrayList<Entry<String, Integer>>)entries;
    }
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics