How to sort Map values by key in Java?

后端 未结 15 2424
野性不改
野性不改 2020-11-22 08:26

I have a Map that has strings for both keys and values.

Data is like following:

\"question1\", \"1\"
\"question9\", \"1\"
\"que

15条回答
  •  暖寄归人
    2020-11-22 09:11

    Using the TreeMap you can sort the map.

    Map map = new HashMap<>();        
    Map treeMap = new TreeMap<>(map);
    for (String str : treeMap.keySet()) {
        System.out.println(str);
    }
    

提交回复
热议问题