import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class SortLinkedHashMapByKey {
public static void main(String[] args) {
// 创建一个LinkedHashMap
LinkedHashMap<Integer,String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put(4, "hello,world");
linkedHashMap.put(3, "abc123");
linkedHashMap.put(1, "huhuhu");
linkedHashMap.put(2, "papapa");
// 将LinkedHashMap的内容转移到TreeMap中
// TreeMap默认按照键的升序排序
Map< Integer,String> sortedMap = new TreeMap<>(linkedHashMap);
// 输出排序后的Map
for (Map.Entry< Integer,String> entry : sortedMap.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}