定义
- Hash table based implementation of the Map interface (基于哈希表的Map接口实现)
什么是哈希表
- 设计精妙、用途广泛的数据结构之一
- 拥有键值对元素的无序集合
- 键的值是唯一的,键对应的值可以通过键来获取、更新或移除
- 无论这个哈希表有多大,这些操作(Insert/Search/Delete)基本上通过常量时机 O(1) 的键比较就可完成
HashMap 实战
Map<String, Double> map = new HashMap<>();
System.out.println("Size of map: " + map.size());
System.out.println("IsEmpty result: " + map.isEmpty());
- 【重点】put/putAll/putIfAbsent
Double oldValue = map.put("abc", 2.3);
System.out.println("The value of abc: " + oldValue);
System.out.println("The current value of abc: " + map.get("abc"));
oldValue = map.put("abcd", 2.56);
System.out.println("The value of abcd: " + oldValue);
System.out.println("The current value of abcd: " + map.get("abcd"));
oldValue = map.put("abc", 2.5);
System.out.println("2nd: The value of abc: " + oldValue);
System.out.println("The current value of abc: " + map.get("abc"));
oldValue = map.put(null, null);
System.out.println("The value of null: " + oldValue);
oldValue = map.put(null, null);
System.out.println("The value of null: " + oldValue);
Double v = map.putIfAbsent("abc", 10.22);
System.out.println("v of abc: " + v);
v = map.putIfAbsent("a", 111.11);
System.out.println("v of abc: " + v);
System.out.println("The value of key a: " + map.get("a"));
System.out.println("The value of key d before putAll: " + map.get("d"));
System.out.println("The value of key e before putAll: " + map.get("e"));
System.out.println("The value of key a before putAll: " + map.get("a"));
Map<String, Double> newMap = new HashMap<>();
newMap.put("d", 100.12);
newMap.put("e", 100.13);
newMap.put("a", 0.1);
map.putAll(newMap);
System.out.println("The value of key d: " + map.get("d"));
System.out.println("The value of key e: " + map.get("e"));
System.out.println("The value of key a: " + map.get("a"));
Double valueOfABC = map.get("abc");
System.out.println();
System.out.println("The value of key abc: " + valueOfABC);
Double valueOfJava = map.get("java");
System.out.println("The value of key java: " + valueOfJava);
map.put("jirengu", null);
System.out.println("The value of key jirengu: " + map.get("jirengu"));
Double valueOfMethodGetOrDefault = map.getOrDefault("a", 10.05);
System.out.println("The value of getOrDefault for key a: " + valueOfMethodGetOrDefault);
valueOfMethodGetOrDefault = map.getOrDefault("method", 0.333333);
System.out.println("The value of getOrDefault for key method: " + valueOfMethodGetOrDefault);
- 【重点】containsKey/containsValue
System.out.println("The result of a: " + map.containsKey("a"));
System.out.println("The result of a: " + map.containsKey("method"));
System.out.println("The result of a: " + map.containsValue(2.5));
System.out.println("The result of a: " + map.containsValue(0.11111111));
System.out.println("The result of a: " + map.containsValue(2.3));
Double valueToRemove = map.remove("a");
System.out.println("Value to remove for key a: " + valueToRemove);
System.out.println("The value of key a: " + map.get("a"));
valueToRemove = map.remove("method");
System.out.println("Value to remove for key method: " + valueToRemove);
boolean isSuccess = map.remove("abc", 100.01);
System.out.println("Flag isSuccess: " + isSuccess);
System.out.println("Size of map: " + map.size());
System.out.println("The value of key abc: " + map.get("abc"));
isSuccess = map.remove("abc", 2.5);
System.out.println("Flag isSuccess: " + isSuccess);
System.out.println("Size of map: " + map.size());
System.out.println("The value of key abc: " + map.get("abc"));
oldValue = map.replace("e", 0.55);
System.out.println("The old value before replacing for key a: " + oldValue);
System.out.println("The current value for key a: " + map.get("e"));
- 【重点】keySet/values/entrySet
Set<String> keys = map.keySet();
for (String key: keys) {
System.out.println("The key in map: " + key);
}
Collection<Double> values = map.values();
for (Double value: values) {
System.out.println("The value in map: " + value);
}
Set<Map.Entry<String, Double>> entries = map.entrySet();
for (Map.Entry<String, Double> entry : entries) {
String key = entry.getKey();
Double value = entry.getValue();
System.out.println("The entry in map: " + key + " value: " + value);
}
Map<Integer, Integer> map = new HashMap<>();
map.put(1, 100);
map.put(2, 200);
map.put(3, 300);
map.put(4, 400);
map.put(5, 500);
// 遍历HashMap
// 1. 遍历entry
for (Map.Entry<Integer, Integer> m : map.entrySet()) {
System.out.println("EntrySet key: " + m.getKey() + " Value: " + m.getValue());
}
// 2. 通过keySet()遍历key,然后利用map.get(key)获取value
for (Integer key : map.keySet()) {
System.out.println("KeySet key: " + key + " Value: " + map.get(key));
}
// 3. 利用迭代器 set的迭代器 (一般工作中较少用到)
// (1) 利用entrySet()迭代器
Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, Integer> entry = iterator.next();
System.out.println("EntrySet Iterator Key: " + entry.getKey() + " Value: " + entry.getValue());
}
// (2) 里利用keySet()迭代器,然后利用map.get(key)获取value
Iterator<Integer> keyIterator = map.keySet().iterator();
while (keyIterator.hasNext()) {
Integer key = keyIterator.next();
System.out.println("KeySet Iterator Key: " + key + " Value: " + map.get(key));
}
// 4. lambda Java8
map.forEach((k, v) -> System.out.println("Lambda Foreach key: " + k + " Value: " + v));
// 啰嗦
map.entrySet().forEach(entry -> System.out.println("Lambda Entry key: " + entry.getKey() + " value: " + entry.getValue()));
map.keySet().forEach(k -> System.out.println("Key: " + k + " Value: " + map.get(k)));