解决方法:

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;
}

 

相关文章:

  • 2021-10-21
  • 2021-10-18
  • 2021-07-26
  • 2021-11-23
  • 2022-01-03
  • 2021-10-12
  • 2021-06-24
  • 2022-01-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2021-09-20
相关资源
相似解决方案