将指定值与映射中的指定键关联。如果映射之前包含键的映射,则替换旧值。
特点
- HashMap 实现了Java Map 和 AbstractMap 接口
- 线程不安全,在并发的时候需要开发者自己控制竞争
- 允许null,null形式
- 无序
重点
- 提供key, value 数据存储
- 支持泛型
示例
//声明保存Key:学生学号Integer和Value:姓名String的HashMap
HashMap<Integer, String> studentHashMap = new HashMap<>();
studentHashMap.put(102, "小王");
studentHashMap.put(103, "小张");
studentHashMap.put(104, "小李");
//输出HashMap的结果 {102=小王, 103=小张, 104=小李}
System.out.println("-------------------------");
System.out.println(studentHashMap);
//声明保存Key:姓名String和Value:学生学号Integer的HashMap
HashMap<String, Integer> studentHashMap1 = new HashMap<>();
studentHashMap1.put("小王", 102);
studentHashMap1.put("小张", 103);
studentHashMap1.put("小李", 104);
//输出HashMap的结果 {小李=104, 小王=102, 小张=103}
System.out.println("-------------------------");
System.out.println(studentHashMap1);
//通过containkey方法检查Key是否存在,通过get方法获得key对应的Value
if (studentHashMap.containsKey(102)) {
System.out.println(studentHashMap.get(102));
}
System.out.println(studentHashMap1.get("小王"));
//如果Key不存在,则返回null
System.out.println(studentHashMap.get(110));
//获得hashmap的所有的key和value
System.out.println("Keys = "+studentHashMap.keySet()); //Keys = [102, 103, 104]
System.out.println("Values = "+studentHashMap.values()); //Values = [小王, 小张, 小李]
其他
示例程序源码 By bluejazzchn