## HashMap
Hashmap是以键值对的方式存储元素,即一个键对应一个元素
★特点:
1.Hashmap能够快速访问和存储元素
2.Hashmap内部逻辑是无序的
3.Hashmap的键和值都可以为null,但只允许一个键为null,因为**Hashmap键是唯一的**
4.Hashmap具有一对一映射关系,适用于直接访问的情形
★常用方法函数
1.创建Hashmap:
(1)简单型:HashMap<K,V> map=new HashMap(); //K,V为键和值的数据类型
(2)复杂型:HashMap<K,HashMap<K,V>>=new HashMap(); //K或V为Map类型
2.添加键值元素:
map.put(key,value)
3.通过键访问值:
map.get(Key)
4.删除某一键值对:
map.remove(Key)
5.是否包含指定对象
map.containsKey() map.containsValue()
6.map是否为空:
map.isEmpty()
7.求map键值对个数:
map.size()
8.遍历HashMap:
(1)遍历所有键:for (K key : map.keySet()) { // 使用 key 进行处理 }
(2)遍历所有值: for (V value : map.values()) { // 使用 value 进行处理 }
(3)遍历键值对:for (Map.Entry<K, V> entry : map.entrySet())
{ K key = entry.getKey(); V value = entry.getValue(); }
9.清空所有键值对:
map.clear()
10.替换值:
map.replace(Key,newValue)