java 前缀匹配_java – 在Map实现中搜索前缀的最佳方法是什么?

在Java中,当Trie数据结构不可用时,对于前缀匹配的需求,通常考虑使用LinkedHashMap。然而,Apache的Patricia Trie提供了更高效的前缀查找、插入和删除操作。Patricia Trie每个节点都存储数据,减少了比较次数,尤其适用于前缀Map操作。在给定的示例中,搜索以'a.'开头的键将返回两个匹配值。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值