Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1,"一");
map.put(3,"三");
map.put(4,"四");
map.put(2,"二");
//要根據(jù)key,和value排序嘛伟桅,用到Collections.sort()的方法,
//首先將map轉(zhuǎn)化為list
List<Map.Entry<Integer,String>> list = new LinkedList<Map.Entry<Integer,String>>(map.entrySet());
//entrySet() 方法/返回map中包含的映射關(guān)系的 Set 視圖情臭。
//Map.Entry表示映射關(guān)系点晴,迭代后可以e.getKey()霍掺,e.getValue()取key和value
//哎匾荆。拌蜘。。
Collections.sort(list,new Comparator<Map.Entry<Integer,String>>(){
? ? ? ? ? ? ? ? ? @Overide
? ? ? ? ? ? ? ? ? public int compare(Map.Entry<Integer,String> o1,Map.Entry<Integer,String> o2){
? ? ? ? ? ? ? ? ? //根據(jù)key排序,根據(jù)value排序就getValue
? ? ? ? ? ? ? ? ? returen 01.getKey() - 02.getKey();//正序棋凳,倒序 o2-o1
? ? ? ? ? ? ? ? ? }
});
print 的list就是排好序的了拦坠,需要的話连躏,在轉(zhuǎn)化成map..