//needToSortMap : 按照value升序排列 Map<String, String> sortedMap = new LinkedHashMap<String, String>(); List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>( needToSortMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<String, String>>() { @Override public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { return o1.getValue().compareTo(o2.getValue()); } }); Iterator<Map.Entry<String, String>> iter = entryList.iterator(); Map.Entry<String, String> tmpEntry = null; while (iter.hasNext()) { tmpEntry = iter.next(); sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue()); } return sortedMap; //map按照key升序排序 /*Map<String,String> sortedMapByKey = new TreeMap<String,String>(); sortedMapByKey.putAll(needToSortMap ); sortedMapByKey = new TreeMap<String,String>(new Comparator<String>() {//第二种 public int compare(String obj1, String obj2) { return obj1.compareTo(obj2); } }); sortedMapByKey.putAll(needToSortMap );*/
java map按照key-value排序
最新推荐文章于 2024-01-26 20:19:06 发布