TableMap
Introduction
Sometimes we need a one-to-one correspondence between keys and values, but there may be duplicate keys or duplicate values, just like a two-column table:
Key | Value |
---|---|
key1 | value1 |
key2 | value2 |
Therefore, Hutool created the TableMap
data structure, which establishes a list separately through the key-value pair to achieve a one-to-one correspondence and enable both forward and reverse lookups.
Of course, whether it’s a forward or reverse lookup, this type of Map involves traversing a list during the search process, which is slower than a standard HashMap, and becomes slower as the amount of data increases.
Usage
TableMap<String, Integer> tableMap = new TableMap<>(new HashMap<>());
tableMap.put("aaa", 111);
tableMap.put("bbb", 222);
// 111
tableMap.get("aaa");
// 222
tableMap.get("bbb");
// aaa
tableMap.getKey(111);
// bbb
tableMap.getKey(222);
// [111]
tableMap.getValues("aaa");
// [aaa]
tableMap.getKeys(111);