Map接口
Map记录的是key和Value间的映射关系。例如男朋友和女朋友这种就能建立起映射关系,如小明(key)的女朋友(映射关系)是小红(Value)
需要注意的一点是Map中的key是不能重复的,而Value是可以重复的。但是一个key只能和一个Value建立映射关系。也就是说,在JDK的世界里,小明的女朋友是小红,老王的女朋友也可以是小红,但是小明的女朋友不能既是小红,又是小兰。
常用方法
V put(K key, V value); // 向Map中添加一组映射关系,并返回这个key之前映射的value
V get(Object key); // 返回key对应的value
V remove(Object key); // 删除一组映射关系,并返回key映射的value
boolean containsKey(Object key); // 是否包含某个key
boolean containsValue(Object value); // 是否包含某个value
int size(); // 保存的映射关系的数量
boolean isEmpty(); // Map是否为空
使用方法:
public static void test1() {
Map<Integer, String> map = new HashMap<>();
System.out.println(map.put(1, "a"));
System.out.println(map);;
System.out.println(map.get(1));
System.out.println(map.size());
if (map.containsValue("a")) {
System.out.println("contains a");
}
System.out.println(map.size());
if (map.containsKey(1)) {
System.out.println(map.remove(1));
}
System.out.println(map.isEmpty());
}
运行结果
null
{1=a}
a
1
contains a
1
a
true
Process finished with exit code 0
遍历一个Map
遍历每个映射关系Entry
Map中使用Entry类来表示key和value的映射关系。例如HashMap中使用的Entry的实现类Node的实现如下
static class Node<K,V> implements Map.Entry<K,V> {
...
final K key;
V value;
...
}
我们可以通过遍历每个Entry来完成对Map的遍历。遍历Entry的方法如下:
public static void traversalEntrySets() {
Map<Integer, String> map = generateMap();
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
for (Map.Entry<Integer, String> e : entrySet) {
System.out.println("key = " + e.getKey() + ", value = " + e.getValue());
}
System.out.println("-----------------------");
map.entrySet().forEach(e -> {
System.out.println("key = " + e.getKey() + ", value = " + e.getValue());
});
System.out.println("-----------------------");
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> e = iterator.next();
System.out.println("key = " + e.getKey() + ", value = " + e.getValue());
}
}
运行结果如下
key = 0, value = a
key = 1, value = b
key = 2, value = c
key = 3, value = d
key = 4, value = e
key = 5, value = f
key = 6, value = g
key = 7, value = h
key = 8, value = i
key = 9, value = j
-----------------------
key = 0, value = a
key = 1, value = b
key = 2, value = c
key = 3, value = d
key = 4, value = e
key = 5, value = f
key = 6, value = g
key = 7, value = h
key = 8, value = i
key = 9, value = j
-----------------------
key = 0, value = a
key = 1, value = b
key = 2, value = c
key = 3, value = d
key = 4, value = e
key = 5, value = f
key = 6, value = g
key = 7, value = h
key = 8, value = i
key = 9, value = j
Process finished with exit code 0
除了直接遍历Entry之外,JDK还提供了遍历key和Value的方法,他们底层都是通过遍历entry实现的。
遍历key
public static Map<Integer, String> generateMap() {
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < 10; i++) {
map.put(i, "" + (char)('a' + i));
}
return map;
}
public static void traversalKeys() {
Map<Integer, String> map = generateMap();
for (Integer key : map.keySet()) {
System.out.println("key = " + key + ", value = " + map.get(key));
}
System.out.println("-----------------------");
map.keySet().forEach(e -> {
System.out.println("key = " + e + ", value = " + map.get(e));
});
System.out.println("-----------------------");
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println("key = " + key + ", value = " + map.get(key));
}
}
运行结果如下
key = 0, value = a
key = 1, value = b
key = 2, value = c
key = 3, value = d
key = 4, value = e
key = 5, value = f
key = 6, value = g
key = 7, value = h
key = 8, value = i
key = 9, value = j
-----------------------
key = 0, value = a
key = 1, value = b
key = 2, value = c
key = 3, value = d
key = 4, value = e
key = 5, value = f
key = 6, value = g
key = 7, value = h
key = 8, value = i
key = 9, value = j
-----------------------
key = 0, value = a
key = 1, value = b
key = 2, value = c
key = 3, value = d
key = 4, value = e
key = 5, value = f
key = 6, value = g
key = 7, value = h
key = 8, value = i
key = 9, value = j
Process finished with exit code 0
遍历Value
public static void traversalValues() {
Map<Integer, String> map = generateMap();
for (String s : map.values()) {
System.out.println(s);
}
System.out.println("-----------------------");
map.values().forEach(e -> {
System.out.println("value = " + e);
});
System.out.println("-----------------------");
Iterator<String> iterator = map.values().iterator();
while (iterator.hasNext()) {
String value = iterator.next();
System.out.println("value = " + value);
}
}
结果如下
a
b
c
d
e
f
g
h
i
j
-----------------------
value = a
value = b
value = c
value = d
value = e
value = f
value = g
value = h
value = i
value = j
-----------------------
value = a
value = b
value = c
value = d
value = e
value = f
value = g
value = h
value = i
value = j
Process finished with exit code 0
实现类
Map常用的实现类有两种:
- HashMap: 无序
- LinkedHashMap: 有序