java 字典
數據結構總覽
datastruct.png
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 的 value -
getOrDefault
: 獲取指定 key 的 value,如果沒有 key,返回默認值 -
containsKey
: 判斷字典是否包含 key -
containsValue
: 判斷字典是否包含 value -
keySet
: key 的集合 -
values
: value 的集合 -
entrySet
: 包含 key/value 的集合,主要用于遍歷 -
put
: 添加一個 key/value -
putIfAbsent
: key 不存在才添加,如果 key 存在,返回 value,如果 key 不存在,返回 null -
putAll
: 合并 map,不存在的 key 添加,已存在的 key 覆蓋 -
remove(key)
: 刪除,返回老 value -
remove(key, val)
: 存在map[key] = val
才刪除,返回是否有元素刪除 -
replace(key, newVal)
: 替換,返回老 value -
replace(key, val, newVal
: 存在map[key] = val
才替換,返回是否有元素替換 -
repalceAll
: 對所有的 key/value 執行BiFounction
替換原來的 value -
compute
: 所選的 key/oldValue 執行BiFounction
替換原來的 value;如果 key 不存在,則 oldValue 為 null -
computeIfPresent
: key 存在才執行BiFounction
替換原來的 value -
computeIfAbsent
: 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
: 最小的 key -
lastKey
: 最大的 key -
headMap
: 小于給定元素的 key 構成的 map -
tailMap
: 大于等于給定元素的 key 構成的 map -
subMap
: 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
: 小于給定值的最大的 key -
higherKey
: 大于給定值的最小的 key -
floorKey
: 小于等于給定值的最大的 key -
ceilingKey
: 大于等于給定值的最小的 key -
lowerEntry
: 小于給定值的最大的 entry -
higherEntry
: 大于給定值的最小的 entry -
floorEntry
: 小于等于給定值的最大的 entry -
ceilingEntry
: 大于等于給定值的最小的 entry -
pollFirstEntry
: 刪除并獲取最小的 entry -
pollLastEntry
: 刪除并獲取最大的 entry -
headSet
: 頭部 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"});
}