LinkedHashMap.put("a.a1","11");
LinkedHashMap.put("a.a12","12");
LinkedHashMap.put("b.b1","13");
LinkedHashMap.put("c.c1","14");
LinkedHashMap.put("c.c1","15");
搜索“a.” key应该返回两个值.
我们应该使用哪种数据结构作为Trie DS实现不可用.我能想到的下一个最好的只是LinkedHashMap
解决方法:
您正在寻找Apache Patricia Trie.它是您的用例的确切数据结构.
从他们的文档:
A PATRICIA Trie is a compressed Trie. Instead of storing all data at the edges of the Trie (and having empty internal nodes), PATRICIA stores data in every node. This allows for very efficient traversal, insert, delete, predecessor, successor, prefix, range, and select(Object) operations. All operations are performed at worst in O(K) time, where K is the number of bits in the largest item in the tree. In practice, operations actually take O(A(K)) time, where A(K) is the average number of bits of all items in the tree.
Most importantly, PATRICIA requires very few comparisons to keys while doing any operation. While performing a lookup, each comparison (at most K of them, described above) will perform a single bit comparison against the given key, instead of comparing the entire key to another key.
特别是,prefixMap(prefix) operation返回一个SortedMap视图,其中包含与给定前缀匹配的所有条目.
再次,从文档:
For example, if the Trie contains ‘Anna’, ‘Anael’, ‘Analu’, ‘Andreas’, ‘Andrea’, ‘Andres’, and ‘Anatole’, then a lookup of ‘And’ would return ‘Andreas’, ‘Andrea’, and ‘Andres’.
标签:java,dictionary,data-structures,string
来源: https://codeday.me/bug/20190519/1135701.html