hashmap
1hashmap是一个散列表,他存储的是键值对(key-value)映射
2hashmap继承AbstractMap 实现了Map,Cloneable,Serializable接口
3HashMap的实现不是同步的,线程不安全,但是效率高
HashMap允许null键和null值,是基于哈希表的Map接口实现
哈希表的作用是用来保证键的唯一性
HashMap的三种遍历方式
第一种 遍历HashMap的entrySet键值对集合1
1通过HashMap.entrySet()得到键值对集合;
2通过迭代器Lterator遍历键值对集合得到key值和value
// 创建一个key和value均为String的Map集合
Map<String, String> map = new HashMap<String, String>();
map.put("1", "11");
map.put("2", "22");
map.put("3", "33");
// 键和值
String key = null;
String value = null;
// 获取键值对的迭代器
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
key = (String) entry.getKey();
value = (String) entry.getValue();
System.out.println("key:" + key + "---" + "value:" + value);
}
第二种:遍历HashMap键的set集合获取值
1通过hashMap.keyset()获取键的Set集合
2遍历键的Set集合获取值
第三种:遍历HashMap“值”的集合;
1.通过HashMap.values()得到“值”的集合
2.遍历“值”的集合;
浙公网安备 33010602011771号