java中map排序

Map<String, Integer> map = new TreeMap<String, Integer>(); map.put("j2se", 20); map.put("j2ee", 10); map.put("j2me", 30); List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>( map.entrySet()); //排序前 for (int i = 0; i < infoIds.size(); i++) { String id = infoIds.get(i).toString(); System.out.println(id); } //排序 Collections.sort(infoIds, 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 < infoIds.size(); i++) { String id = infoIds.get(i).toString(); System.out.println(id); } 排序前: j2ee=10 j2me=30 j2se=20 排序后: j2me=30 j2se=20 j2ee=10

你可能感兴趣的:(java,String,J2SE,Integer,j2me)