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

posted @ 2020-03-19 17:17  那些年的代码  阅读(6212)  评论(0编辑  收藏  举报