- 使用HashMap类的构造方法创建一个空的HashMap对象:
HashMap<String, Integer> map = new HashMap<>();- 使用HashMap类的构造方法创建一个包含指定初始容量和负载因子的HashMap对象:
HashMap<String, Integer> map = new HashMap<>(16, 0.75f);- 使用HashMap类的put方法逐个添加键值对:
HashMap<String, Integer> map = new HashMap<>();map.put("A", 1);map.put("B", 2);map.put("C", 3);- 使用Collections类的静态方法singletonMap创建一个包含单个键值对的不可修改的Map对象:
Map<String, Integer> map = Collections.singletonMap("key", value);上一篇:C++中封装的作用是什么
下一篇:C++移动语义的优势有哪些
hashmap









