java-字典

Java HashMap

参考: https://www.runoob.com/java/java-hashmap.html

1、添加元素 (put)

方法一:

Map m1 = new HashMap();
m1.put("Zara", "8");

m1.put(2, 8);
System.out.print(m1);

方法二:

// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Runoob");
Sites.put(3, "Taobao");
Sites.put(4, "Zhihu");
System.out.println(Sites);

2、删除元素

Sites.remove(4);

删除所有,键值对

Sites.clear();

3、获取元素(get)

Sites.get(3)

4、修改元素 

用put 和 添加的方法一样

 还有api可以调用 

https://www.runoob.com/java/java-hashmap-replace.html

5、计算元素的数量

Sites.size()

原文地址:https://www.cnblogs.com/kaibindirver/p/15316919.html