Java HashMap转TreeMap

Map<String, String> testMap = new HashMap<String, String>();
testMap.put("1", "3");
testMap.put("3", "4");
testMap.put("2", "1");
testMap.put("4", "2");
for (String key : testMap.keySet()) {
    System.out.println(key + "=>" + testMap.get(key));
}
System.out.println("========================");
Map<String, String> testMap2 = new TreeMap<String, String>(testMap);
for (String key : testMap2.keySet()) {
    System.out.println(key + "=>" + testMap2.get(key));
}

3=>4
2=>1
1=>3
4=>2
========================
1=>3
2=>1
3=>4
4=>2

原文地址:https://www.cnblogs.com/zhuyeshen/p/12525633.html