应用--对HashMap进行排序(转为LinkedHashMap)
解决方法:
Map中LinkedhashMap是有序的,将HashMap转化为LinkedHashMap,可以达到要求。
public Static Map<String,String> sortHashMap(Map<String,String> map){ Map<String,String> sortedMap = new LinkedHashMap<>(); List<String> list = new ArrayList<>(); Iterator<String> item = map.keySet.iterator(); while(item.hasNext()){ list.add(item.next()); } Collections.sort(list); Iterator<String> item2 = list.iterator(); while(item2.hasNext()){ String key = item2.next(); sortedMap.put(key,map.get(key)); } return sortedMap; }