Map接口方法一览
以下方法均以Map接口实现类HashMap演示,因为这些方法对实现了Map接口的所有实体类都适用
1
| Map<String, String> map = new HashMap(10);
|
put
put 方法用于将(K,V)键值对放入Map集合中
返回旧值
首次插入时旧值为null,之后value存在就返回这个插入之前的value
1 2 3 4 5 6 7 8
|
System.out.println(A.put("name", "ikun"));
System.out.println(A.put("name", "ikun"));
|
get
取出K在Map集合中对应的V
1 2 3 4
| System.out.println("name: " + map.get("name"));
|
putall
将一个Map集合(这里称为A集合)中所有的(K,V)键值对放入另外一个Map集合中(这里称为B集合)
注意:这里A集合中的键值对并不会消失
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Map<String, String> A = new HashMap<>(10);
A.put("name", "ikun"); A.put("age", "18"); A.put("height", "180cm"); A.put("weight", "70kg");
Map<String, String> B = new HashMap<>(10);
B.putAll(A);
System.out.println(A); System.out.println(B);
|
size
返回Map集合中(K,V)键值对的个数
1 2 3 4
| System.out.println(A.size());
|
isEmpty
判断Map集合是否为空
空则返回true,不空则返回false
1 2 3 4 5 6 7 8 9 10 11
| System.out.println(A.isEmpty());
Map<String, String> C = new HashMap<>(10);
System.out.println(C.isEmpty());
|
containsKey
判断Map集合中是否存在指定Key
存在返回true,不存在返回false
1 2 3 4 5 6 7
| System.out.println(A.containsKey("name"));
System.out.println(A.containsKey("hobby"));
|
containsValue
判断Map集合中是否存在指定Value
存在返回true,不存在返回false
1 2 3 4 5 6 7
| System.out.println(A.containsValue("ikun"));
System.out.println(A.containsValue("rap"));
|
remove
用于移除Map集合中指定的(K,V)键值对
该方法有两个重载
-
String remove(Object key)
用于移除指定Key对应的键值对,返回移除键值对的Value
-
boolean remove(Object key, Object value)
用于移除指定(key,value)键值对,移除成功返回true,移除失败返回false
当且仅当键值对(key,value)存在时才能移除成功
1 2 3 4 5 6 7 8 9
| System.out.println(A.remove("weight"));
System.out.println(A.remove("age", "18"));
System.out.println(A.remove("name", "xiaoming"));
|
replace
用于修改Map集合(K,V)键值对K对应的V
该方法有两个重载
-
String replace(String key, String value)
key指定修改哪个键值对,value为键值对(K,V)的V修改后的值
返回值为修改前key对应的value字符串
-
boolean replace(String key, String oldValue, String newValue)
指定修改Map集合中(key, oldValue)为(key, newValue)
当且仅当(key, oldValue)存在时才可修改成功
修改成功返回true,修改失败返回false
1 2 3 4 5 6 7
| System.out.println(B.replace("name", "小明"));
System.out.println(B.replace("name", "小明", "ikun"));
|
keySet
得到Map集合中key的集合
1 2 3 4 5 6
| Set<String> keys = B.keySet();
System.out.println(keys);
|
values
得到Map集合中value的集合
1 2 3 4 5 6
| Collection<String> values = B.values();
System.out.println(values);
|
entrySet
得到map集合的entrySet
1 2 3 4 5 6
| Set<Map.Entry<String, String>> entrySet = B.entrySet();
System.out.println(entrySet);
|
hashCode
返回Map集合的hashCode
1 2 3 4
| System.out.println(B.hashCode());
|
putIfAbsent
和put一样,用于将(K,V)键值对放入Map集合中
但是putIfAbsent只会在(K,V)键值对不存在于Map集合中时才能放入,这时返回null
(K,V)键值对存在时不能放入,这时返回已经存在的V
1 2 3 4 5 6 7
| System.out.println(B.putIfAbsent("hobby", "rap"));
System.out.println(B.putIfAbsent("name", "小明"));
|
getOrDefault
对不存在于Map集合中的(K,V)键值对提供默认值
1 2 3 4 5 6 7
|
System.out.println(B.getOrDefault("sex", "男"));
B.put("sex", "女");
System.out.println(B.getOrDefault("sex", "男"));
|
equals
判断两个Map集合是否相等
包括键值对个数和Map集合中的键值对要相等
结果证明:如果Map集合满足键值对个数和Map集合中的键值对相同,它们的hashCode也是一样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| Map<String, String> A = new HashMap<>(10); Map<String, String> B = new HashMap<>(10); Map<String, String> C = new HashMap<>(10);
A.put("name", "ikun"); A.put("age", "18"); A.put("height", "180cm"); A.put("weight", "70kg"); B.put("name", "ikun"); B.put("age", "18"); B.put("height", "180cm"); B.put("weight", "70kg"); C.put("name", "ikun"); C.put("age", "18"); C.put("height", "180cm"); C.put("weight", "70kg");
System.out.println("A: " + A + " hashCode = " + A.hashCode()); System.out.println("B: " + B + " hashCode = " + B.hashCode()); System.out.println("C: " + C + " hashCode = " + C.hashCode());
System.out.println("A equals B? " + A.equals(B)); System.out.println("A equals C? " + A.equals(C)); System.out.println("B equals C? " + B.equals(C));
|
clear
清空集合中所有的键值对
结果证明:Map中的hashCode随键值对变化而变化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Map<String, String> D = new HashMap<>(10);
D.put("name", "ikun"); D.put("age", "18"); D.put("height", "180cm"); D.put("weight", "70kg");
System.out.println("D: " + D + " hashCode = " + D.hashCode());
D.clear();
System.out.println("D: " + D + " hashCode = " + D.hashCode());
|