import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer,String> m = new HashMap<Integer,String>();
m.put(1934, "意大利");
m.put(1938, "意大利");
m.put(1950, "乌拉圭");
m.put(1954, "西德");
m.put(1958, "巴西");
m.put(1962, "巴西");
m.put(1966, "英格兰");
List<Map.Entry<Integer, String>> list =new LinkedList<Map.Entry<Integer,String>>(m.entrySet());
Collections.sort(list,new Comparator<Map.Entry<Integer, String>>() {//根据Value排序
@Override
public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
Map<Integer,String> mp= new LinkedHashMap<Integer, String>();
for(Map.Entry<Integer,String> entry:list) {
mp.put(entry.getKey(), entry.getValue());
}
System.out.println(m);
for (Entry<Integer, String> entry : mp.entrySet()) { //遍历
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
Map的遍历和排序
最新推荐文章于 2023-01-09 11:04:30 发布