将键映射到值的对象。
一个映射不能包含重复的键;
每个键最多只能映射到一个值。
实现类: HashMap, TreeMap, Hashtable,
从以下版本开始:1.2
注意
1:当有重复的键的时候,后面的值会将前面的值覆盖
2:可以添加null键null值,但是只能添加一对
3:值可以重复
4:map集合主要是和键有关系,只要找到键,就能根据键找到值
₯
Map集合其实就是由Set和List构成的
key是Set集合 不能有重复
Value是List集合 可以有重复
集合常用方法
添加 put(k,v)
删除 remove(k)
清空 clear();
修改 ???
查询 get(k) 根据键查找值
长度:size();
判断
containsKey 判断是否包含指定的键
containsValue 判断是否包含指定的值
isEmpty 判断集合是否为空
遍历
public static void main(String[] args) {
//1:创建Map集合
HashMap map = new HashMap<>();
//2:给集合添加键值对
map.put("超", "俪");
map.put("明", "颖");
map.put("章", "琍");
map.put("章", "姚");
map.put("强", "蓉");
map.put("哲", "蓉");
map.put(null, null);
map.put(null, null);
System.out.println(map);
//2:删除键值对 根据键删除键值对
map.remove("章");
System.out.println("删除后:"+map);
//3:清空键值对
//map.clear();
//System.out.println(map);
//4:根据键查找值 如果此映射不包含该键的映射关系,则返回 null。
System.out.println(map.get("超1"));
//5:获取键值对的对数
System.out.println(map.size());
//6:判断是否包含指定的键
System.out.println(map.containsKey("超"));
//7:判断是否包含指定的值
System.out.println(map.containsValue("俪"));
//8 isEmpty 判断集合是否为空
System.out.println(map.isEmpty());
//9 返回所有的键的set集合
/*System.out.println("---------------------");
Set set = map.keySet();
for (String s : set) {
System.out.println(s);
}
System.out.println("---------------------");
//10 返回所有的值的Collection集合
Collection coll = map.values();
for (String s : coll) {
System.out.println(s);
}*/
System.out.println("---------------------");
//11 遍历键值对1
//11.1 现获取所有的键
Set set = map.keySet();
//11.2 遍历所有的键
for (String key : set) {
//11.3 遍历一个键,然后在根据键获取值
String value = map.get(key);
System.out.println(key+":"+value);
}
System.out.println("---------------------");
//12 遍历键值对2
Set> entrySet = map.entrySet();
for(Map.Entry s:entrySet){
System.out.println(s.getKey()+":"+s.getValue());
}
}
基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(
此类不保证映射的顺序,特别是它不保证该顺序恒久不变。
注意,此实现不是同步的。
父接口:Map
子类:LinkedHashMap
需求1:集合保存,
注意:数字可以写10进制,8进制,16进制,2进制
2进制 前缀是0b jdk1.8 新特性
8进制 前缀是0
10进制 直接写数字
16进制 前缀是0x;
public static void main(String[] args) {
//int money = 100_123_456;
//1:创建一个HashMap集合
HashMap hm = new HashMap<>();
hm.put(0b000001100, "马云");
hm.put(0x1, "马化腾");
hm.put(0x2, "柳传志");
hm.put(0x3, "雷军");
hm.put(0x4, "李宏毅");
hm.put(0x5, "求伯君");
hm.put(0x6, "求伯君");
hm.put(0x7, "求伯君");
hm.put(0xff, "求伯君");
//hm.put(08, "求伯君");
System.out.println(hm);
}
基于红黑树(Red-Black tree)的 NavigableMap 实现。
○ 1 该映射根据其键的自然顺序进行排序
○ 2 根据创建映射时提供的 Comparator 进行排序
○具体取决于使用的构造方法。
○ 注意,此实现不是同步的。
○ 从以下版本开始: 1.2
○ TreeMap:底层是基于红黑树的二叉树
○ 排序且唯一
与HashMap相似
public static void main(String[] args) {
//1:创建TreeMap集合
TreeMap tm = new TreeMap<>();
//2:添加键值对
tm.put(10, "唐朝");
tm.put(5, "宋朝");
tm.put(7, "元朝");
tm.put(2, "明朝");
tm.put(1, "清朝");
tm.put(10, "汉朝");
System.out.println(tm);
}
Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序。
父类:HashMap
注意,此实现不是同步(不安全,效率高)的。
从以下版本开始: 1.4
public static void main(String[] args) {
LinkedHashMap lhm = new LinkedHashMap<>();
lhm.put("a", 1001);
lhm.put("d", 1002);
lhm.put("c", 1003);
lhm.put("t", 1004);
lhm.put("y", 1005);
//1:获取所有的键的集合
Set set = lhm.keySet();
//2:遍历键
for (String key : set) {
//3:根据键获取值
Integer value = lhm.get(key);
System.out.println(key+":"+value);
}
System.out.println("---------------");
//1:获取集合的映射关系
Set> entrySet = lhm.entrySet();
//2:遍历映射关系
for(Entry en:entrySet){
//3:分别获取键和值
System.out.println(en.getKey()+":"+en.getValue());
}
System.out.println(lhm);
}
:
此类实现一个哈希表,该哈希表将键映射到相应的值。任何非 null 对象都可以用作键或值。
父接口:Map
从以下版本开始: JDK1.0
Hashtable 是同步的
面试题:HashMap和Hashtable的区别?
1 产生时间
Hashtable jdk1.0
HashMap jdk1.2
2 继承的父类不同
Hashtable父类:Dictionary
HashMap父类:AbstractMap
3 对外提供的接口不同
Hashtable比HashMap多提供了elments() 和contains() 两个方法。
4 对Null key 和Null value的支持不同
Hashtable 不支持null键和null值
HashMap 支持
5 线程安全性不同
Hashtable 同步
HashMap 不同步
6 遍历方式的内部实现上不同
Hashtable、HashMap都使用了 Iterator。
而由于历史原因,Hashtable还使用了Enumeration的方式 。
7 初始容量大小和每次扩充容量大小的不同
Hashtable默认的初始大小为11,之后每次扩充,容量变为原来的2n+1。
HashMap默认的初始化大小为16。之后每次扩充,容量变为原来的2倍。
public static void main(String[] args) {
Hashtable ht = new Hashtable<>();
ht.put(null, null);
System.out.println(ht);
}