Map<K,V>
K:键 - (任意类型) - (不可重复) - 可为null
V:值 - (任意类型) - (可重复) - 可为null
HashMap<K,V>
常用方法
| 方法 | 作用 | 返回值 |
|---|---|---|
| put(键,值) | 添加键值对 | V |
| get(键) | 键找值 | V |
| remove(键) | 键删键值对 | V |
| replace(键,新值) | 替换值 | V |
| containsKey(key) | 包含键? | boolean |
| containsValue(value) | 包含值? | boolean |
| keySet() | 键集合 | Set<K> |
| values() | 值集合 | Collection<V> |
| entrySet() | 键值对集合 | Set<Entry> |
Entry
保存一组键值对的类型
K getKey() 获取当前键值对对象的键
V getValue() 获取当前键值对对象的值
通过HashMap的entrySet() 获取到的Set<Entry<K,V>>集合 就是HashMap指向的地址
通过Set<Entry<K,V>> 去操作(修改值 删除)每一组Entry 都会影响到HashMap
键遍历集合
Set<String> s = hm.keySet();
Iterator<String> it = s.iterator();
while(it.hasNext()){
String name = it.next();
syso(name+":"+hm.get(name));
}
映射关系便利集合
Set<Entry<String, Integer>> s = hm.entrySet();
Iterator<Entry<String, Integer>> it = s.iterator();
while(it.hasNext()) {
Entry<String, Integer> next = it.next();
System.out.println(next.getKey()+" "+next.getValue());
}

被折叠的 条评论
为什么被折叠?



