package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test
{
public static void main(String[] args)
{
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("1", 1);
map.put("3", 3);
map.put("2", 2);
List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
Collections.sort(entryList,
new Comparator<Map.Entry<String, Integer>>()
{
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o2.getValue() - o1.getValue());
}
});
for (int i = 0; i < entryList.size(); i++)
{
Map.Entry<String, Integer> entry = entryList.get(i);
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
HashMap排序
最新推荐文章于 2022-01-19 11:53:18 发布