Partial string search in HashMap key

If you need to do a partial search in a Map key, then this post is for you. An example might be searching a book title from a large library Map, where book title is the key and BookInfo object is the value of the map. The user will input prefix of the book name and based on the partial prefix your map will return a subset.

In new Apache Commons Collection 4.0 release a new data structure has been introduced, called Trie which is basically an ordered tree data structure with a dynamic set of string based keys were all the descendant node holds the common prefix.
See more in the Wikipedia article about Trie.

Commons Collection 4.0 also provides an implementation for PatriciaTrie. Here is an example how to use it in your program.

Even though PatriciaTrie api javadoc mentions about some useful methods like select(), selectKey(), but not sure why I am not able to access those methods in the code. But certainly the prefixMap() method was enough to solve my problem.

Show Comments