在Java中将HashMap转换为TreeMap的程序
首先创建一个HashMap-
Map<String, String> map = new HashMap<>();
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("4", "Four");
map.put("5", "Five");
map.put("6", "Six");现在,将上面的HashMap转换为TreeMap-
Map<String, String> treeMap = new TreeMap<>(); treeMap.putAll(map);
示例
以下是在Java中将HashMap转换为TreeMap的程序-
import java.util.*;
import java.util.stream.*;
public class Demo {
public static void main(String args[]) {
Map<String, String> map = new HashMap<>();
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("4", "Four");
map.put("5", "Five");
map.put("6", "Six");
map.put("7", "Seven");
map.put("8", "Eight");
map.put("9", "Nine");
System.out.println("HashMap = " + map);
Map<String, String> treeMap = new TreeMap<>();
treeMap.putAll(map);
System.out.println("TreeMap (HashMap to TreeMap) " + treeMap);
}
}输出结果
HashMap = {1=One, 2=Two, 3=Three, 4=Four, 5=Five, 6=Six, 7=Seven, 8=Eight, 9=Nine}
TreeMap (HashMap to TreeMap) {1=One, 2=Two, 3=Three, 4=Four, 5=Five, 6=Six, 7=Seven, 8=Eight, 9=Nine}热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志