Java Map按Key值进行排序
程序员文章站
2022-05-10 12:37:06
...
public class MapSortDemo {
public static void main(String[] args) {
Map<String, String> map = new TreeMap<String, String>();
map.put("KFC", "kfc");
map.put("WNBA", "wnba");
map.put("NBA", "nba");
map.put("CBA", "cba");
Map<String, String> resultMap = sortMapByKey(map); //按Key进行排序
// Map<String, String> resultMap = sortMapByValue(map); //按Value进行排序
for (Map.Entry<String, String> entry : resultMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
/**
* 使用 Map按value进行排序
* @param map
* @return
*/
public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {
if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, String> sortedMap = new LinkedHashMap<String, String>();
List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
oriMap.entrySet());
Collections.sort(entryList, new MapValueComparator());
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;
}
}
class MapValueComparator implements Comparator<Map.Entry<String, String>> {
@Override
public int compare(Entry<String, String> me1, Entry<String, String> me2) {
return me1.getValue().compareTo(me2.getValue());
}
}
public static void main(String[] args) {
Map<String, String> map = new TreeMap<String, String>();
map.put("KFC", "kfc");
map.put("WNBA", "wnba");
map.put("NBA", "nba");
map.put("CBA", "cba");
Map<String, String> resultMap = sortMapByKey(map); //按Key进行排序
// Map<String, String> resultMap = sortMapByValue(map); //按Value进行排序
for (Map.Entry<String, String> entry : resultMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
/**
* 使用 Map按value进行排序
* @param map
* @return
*/
public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {
if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, String> sortedMap = new LinkedHashMap<String, String>();
List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
oriMap.entrySet());
Collections.sort(entryList, new MapValueComparator());
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;
}
}
class MapValueComparator implements Comparator<Map.Entry<String, String>> {
@Override
public int compare(Entry<String, String> me1, Entry<String, String> me2) {
return me1.getValue().compareTo(me2.getValue());
}
}
推荐阅读
-
Python实现字典按key或者value进行排序操作示例【sorted】
-
java8 stream 操作map根据key或者value排序的实现
-
Python实现对字典分别按键(key)和值(value)进行排序的方法分析
-
Python实现字典按key或者value进行排序操作示例【sorted】
-
java8 stream 操作map根据key或者value排序的实现
-
Java按照key值字典排序进行MD5加密
-
java 使用面向对象方式实现录入学生信息,取出成绩最大值、最小值、平均值、对其进行排序
-
Java中list里面存放map,根据map中的某一个字段进行排序
-
对map进行排序-按字符出现次数 排序
-
使用Java8 Stream API对Map按键或值进行排序