Java Map 按Key排序和按Value排序【转】【补】 编辑编辑编辑编辑编辑
方式一
package kingtool.sort; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.Map.Entry; /** * * @author King * */ public class MapSortTool { public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(); map.put("4", "2"); map.put("1", "1"); map.put("9", "3"); map.put("8", "6"); // 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按key进行排序 * * @param map * @return */ public static Map<String, String> sortMapByKey(Map<String, String> map) { if (map == null || map.isEmpty()) { return null; } Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyStringComparator()); sortMap.putAll(map); return sortMap; } /** * 使用 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 MapValueStringComparator()); 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; } /** * string key升序 * @author King * */ static class MapKeyStringComparator implements Comparator<String>{ @Override public int compare(String str1, String str2) { return str1.compareTo(str2); } } /** * string value升序 * @author King * */ static class MapValueStringComparator implements Comparator<Map.Entry<String, String>> { @Override public int compare(Entry<String, String> entry1, Entry<String, String> entry2) { return entry1.getValue().compareTo(entry2.getValue()); } } /** * tip: 样例中没用到<br/> * double value降序 * @author King * */ static class MapValueNumberComparator implements Comparator<Map.Entry<String, Double>> { @Override public int compare(Entry<String, Double> entry1, Entry<String, Double> entry2) { return entry1.getValue().compareTo(entry2.getValue()); } } }
方式二
package com.rosellete.iescp;
import com.alibaba.fastjson.JSONObject;
import java.util.*;
public class SortDemo {
public static void main(String[] args) {
String A = "{\n" +
" \"phone\": \"13777777777\",\n" +
" \"type\": \"1\",\n" +
" \"ob\": \"orderDetail\",\n" +
" \"token\": \"aaa\",\n" +
" \"id\": \"13457aeewer\",\n" +
" \"orderId\": \"33023423\",\n" +
" \"timestamp\": \"2024-03-19 01:02:03\",\n" +
" \"sign\": \"aabe332p34\"\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(A);
Collection<String> keyset = jsonObject.keySet();
List<String> list = new ArrayList<String>(keyset);
Collections.sort(list);
Map<String, Object> objectObjectHashMap = new LinkedHashMap<>();
for (int i = 0; i < list.size(); i++) {
objectObjectHashMap.put(list.get(i), jsonObject.get(list.get(i)));
System.out.println("key键---值:" + list.get(i) + "," + jsonObject.get(list.get(i)));
}
objectObjectHashMap.forEach((s, o) -> System.out.println("key = "+s + " ; value = " + o));
}
}
作者:whatlonelytear
本文地址:https://www.cnblogs.com/whatlonelytear/p/6796759.html
欢迎转载,请在明显位置标明出处及链接。
感觉空虚寂寞,只是因为你无所关注,无处付出。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步