java 字典
數據結構總覽
Map
Map
描述的是一種映射關係,一個 key 對應一個 value,可以添加,刪除,修改和獲取 key/value,util 提供了多種 Map
HashMap
: hash 表實現的 map,插入刪除查找性能都是 O(1),key 沒有順序TreeMap
: 紅黑樹實現的 map,插入刪除查找都是 O(lgn),key 按從大到小順序排列Hashtable
: hash 實現,線程安全,key 和 value 都不能爲空,key 沒有順序LinkedHashMap
: hash + 鏈表實現,按插入順序排序IdentityHashMap
: 判斷 key 相等的條件是,兩個引用指向同一個對象,即key == e.key
WeakHashMap
: 弱引用 map,不會獲取數據的強引用,當數據被 GC 清理時,數據將被刪除
Map
的主要接口如下:
isEmpty
: 判斷是否沒有元素size
: 獲取元素個數get
: 獲取指定 key 的 valuegetOrDefault
: 獲取指定 key 的 value,如果沒有 key,返回默認值containsKey
: 判斷字典是否包含 keycontainsValue
: 判斷字典是否包含 valuekeySet
: key 的集合values
: value 的集合entrySet
: 包含 key/value 的集合,主要用於遍歷put
: 添加一個 key/valueputIfAbsent
: key 不存在才添加,如果 key 存在,返回 value,如果 key 不存在,返回 nullputAll
: 合併 map,不存在的 key 添加,已存在的 key 覆蓋remove(key)
: 刪除,返回老 valueremove(key, val)
: 存在map[key] = val
才刪除,返回是否有元素刪除replace(key, newVal)
: 替換,返回老 valuereplace(key, val, newVal
: 存在map[key] = val
才替換,返回是否有元素替換repalceAll
: 對所有的 key/value 執行BiFounction
替換原來的 valuecompute
: 所選的 key/oldValue 執行BiFounction
替換原來的 value;如果 key 不存在,則 oldValue 爲 nullcomputeIfPresent
: key 存在才執行BiFounction
替換原來的 valuecomputeIfAbsent
: key 不存在纔對 key 執行Founction
作爲 value 插入merge
: 用 oldValue 和 newValue 執行BiFounction
替換原來的 value;如果 key 不存在,則 oldValue 爲 null
{
Map<String, String> map = new HashMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2", "key3", "val3"
));
assertEquals(map.size(), 4);
assertFalse(map.isEmpty());
assertTrue(map.containsKey("key3"));
assertTrue(map.containsValue("val3"));
assertEquals(map.get("key3"), "val3");
assertEquals(map.get("key6"), null);
assertEquals(map.getOrDefault("key3", "defaultValue"), "val3");
assertEquals(map.getOrDefault("key6", "defaultValue"), "defaultValue");
assertThat(map.keySet(), equalTo(Set.of("key0", "key1", "key2", "key3")));
assertThat(map.values(), hasItems("val0", "val1", "val2", "val3"));
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
map.forEach((k, v) -> System.out.println(k + " => " + v));
map.clear();
assertTrue(map.isEmpty());
}
{
Map<String, String> map = new HashMap<>();
map.put("key0", "val0");
map.putAll(Map.of("key1", "val1", "key2", "val2"));
assertEquals(map.putIfAbsent("key3", "val3"), null);
assertEquals(map.putIfAbsent("key3", "val33"), "val3");
assertThat(map, equalTo(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2", "key3", "val3"
)));
}
{
Map<String, String> map = new HashMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2", "key3", "val3"
));
assertEquals(map.remove("errorKey"), null);
assertEquals(map.remove("key0"), "val0");
assertFalse(map.remove("key1", "errorValue"));
assertTrue(map.remove("key1", "val1"));
assertThat(map, equalTo(Map.of(
"key2", "val2", "key3", "val3"
)));
}
{
Map<String, String> map = new HashMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2", "key3", "val3"
));
assertEquals(map.replace("errorKey", "replaceValue"), null);
assertEquals(map.replace("key0", "replaceValue"), "val0");
assertFalse(map.replace("key1", "errorValue", "replaceValue"));
assertTrue(map.replace("key1", "val1", "replaceValue"));
assertThat(map, equalTo(Map.of(
"key0", "replaceValue", "key1", "replaceValue", "key2", "val2", "key3", "val3"
)));
}
{
Map<String, String> map = new HashMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2", "key3", "val3"
));
map.replaceAll((k, v) -> k + v);
assertThat(map, equalTo(Map.of(
"key0", "key0val0", "key1", "key1val1", "key2", "key2val2", "key3", "key3val3"
)));
}
{
Map<String, String> map = new HashMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2"
));
assertEquals(map.compute("key0", (k, v) -> k + v), "key0val0");
assertEquals(map.computeIfPresent("key1", (k, v) -> k + v), "key1val1");
assertEquals(map.computeIfPresent("key6", (k, v) -> k + v), null);
assertEquals(map.computeIfAbsent("key2", k -> k + k.replace("key", "val")), "val2");
assertEquals(map.computeIfAbsent("key3", k -> k + k.replace("key", "val")), "key3val3");
assertThat(map, equalTo(Map.of(
"key0", "key0val0", "key1", "key1val1", "key2", "val2", "key3", "key3val3"
)));
}
{
Map<String, String> map = new HashMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2"
));
assertEquals(map.merge("key0", "newVal", (oldValue, newValue) -> (oldValue + "->" + newValue)), "val0->newVal");
assertEquals(map.merge("key3", "newVal", (oldValue, newValue) -> (oldValue + "->" + newValue)), "newVal");
assertThat(map, equalTo(Map.of(
"key0", "val0->newVal", "key1", "val1", "key2", "val2", "key3", "newVal"
)));
}
Hashtable
Hashtable
的 key/value 都不允許爲空
{
Map<Integer, Integer> map = new HashMap<>();
assertDoesNotThrow(() -> map.put(null, 1));
assertDoesNotThrow(() -> map.put(1, null));
}
{
Map<Integer, Integer> map = new Hashtable();
assertThrows(NullPointerException.class, () -> map.put(null, 1));
assertThrows(NullPointerException.class, () -> map.put(1, null));
}
IdentityHashMap
IdentityHashMap
判斷相等的條件是 key 和 entry.key 是否爲同一個引用對象
Map<String, String> map = new IdentityHashMap<>();
String key1 = new String("key1");
map.put(key1, "val1");
assertFalse(key1 == "key1");
assertTrue(key1.equals("key1"));
assertEquals(map.get(key1), "val1");
assertEquals(map.get("key1"), null);
WeakHashMap
WeakHashMap
的 key 爲弱引用,當原對象被 GC 回收時,這個 key 也會被自動刪除
{
Map<String, String> map = new WeakHashMap<>();
String key1 = new String("key1");
map.put(key1, "val1");
assertEquals(map.get("key1"), "val1");
key1 = null;
System.gc();
assertEquals(map.get("key1"), null);
}
{
Map<String, String> map = new WeakHashMap<>();
String val1 = new String("val1");
map.put("key1", val1);
assertEquals(map.get("key1"), "val1");
val1 = null;
System.gc();
assertEquals(map.get("key1"), "val1");
}
SortedMap
SortedMap
繼承自 Map
,key 是有序的,提供了順序相關的幾個接口
firstKey
: 最小的 keylastKey
: 最大的 keyheadMap
: 小於給定元素的 key 構成的 maptailMap
: 大於等於給定元素的 key 構成的 mapsubMap
: from 和 to 之間的元素構成的 map,包含 from 不包含 to
SortedMap<String, String> map = new TreeMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2",
"key3", "val3", "key4", "val4"
));
assertEquals(map.firstKey(), "key0");
assertEquals(map.lastKey(), "key4");
assertThat(map.headMap("key2").keySet(), equalTo(Set.of("key0", "key1")));
assertThat(map.tailMap("key3").keySet(), equalTo(Set.of("key3", "key4")));
assertThat(map.subMap("key2", "key3").keySet(), equalTo(Set.of("key2")));
NavigableMap
繼承自 SortedMap
,提供瞭如下幾個接口
lowerKey
: 小於給定值的最大的 keyhigherKey
: 大於給定值的最小的 keyfloorKey
: 小於等於給定值的最大的 keyceilingKey
: 大於等於給定值的最小的 keylowerEntry
: 小於給定值的最大的 entryhigherEntry
: 大於給定值的最小的 entryfloorEntry
: 小於等於給定值的最大的 entryceilingEntry
: 大於等於給定值的最小的 entrypollFirstEntry
: 刪除並獲取最小的 entrypollLastEntry
: 刪除並獲取最大的 entryheadSet
: 頭部 Map,提供額外參數是否包含給定值tailSet
: 尾部 Map,提供額外參數是否包含給定值subSet
: 子 Map,提供額外參數是否包含特定值
{
NavigableMap<String, String> map = new TreeMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2",
"key3", "val3", "key4", "val4"
));
assertEquals(map.lowerKey("key3"), "key2");
assertEquals(map.higherKey("key3"), "key4");
assertEquals(map.floorKey("key3"), "key3");
assertEquals(map.ceilingKey("key3"), "key3");
assertEquals(map.lowerEntry("key3").getKey(), "key2");
assertEquals(map.higherEntry("key3").getKey(), "key4");
assertEquals(map.floorEntry("key3").getKey(), "key3");
assertEquals(map.ceilingEntry("key3").getKey(), "key3");
map.remove("key3");
assertEquals(map.floorKey("key3"), "key2");
assertEquals(map.ceilingKey("key3"), "key4");
assertEquals(map.floorEntry("key3").getKey(), "key2");
assertEquals(map.ceilingEntry("key3").getKey(), "key4");
}
{
NavigableMap<String, String> map = new TreeMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2",
"key3", "val3", "key4", "val4"
));
assertEquals(map.pollFirstEntry().getKey(), "key0");
assertArrayEquals(map.keySet().toArray(), new String[]{"key1", "key2", "key3", "key4"});
assertEquals(map.pollLastEntry().getKey(), "key4");
assertArrayEquals(map.keySet().toArray(), new String[]{"key1", "key2", "key3"});
}
{
NavigableMap<String, String> map = new TreeMap<>(Map.of(
"key0", "val0", "key1", "val1", "key2", "val2",
"key3", "val3", "key4", "val4"
));
assertArrayEquals(map.headMap("key2", true).keySet().toArray(), new String[]{"key0", "key1", "key2"});
assertArrayEquals(map.tailMap("key3", false).keySet().toArray(), new String[]{"key4"});
assertArrayEquals(map.subMap("key2", false, "key3", true).keySet().toArray(), new String[]{"key3"});
}