I want to search a hash map depending on the user input. Suppose a user give value 'A',I have to display starting with A company name and if user give value 'AB' I have to display starting with AB company name. I am storing company name in hash map
Use a NavigableSet.
Example:
NavigableSet<String> company=new TreeSet<String>();
Set<String> filteredSet=company.tailSet(prefix);
for(String str:filteredSet) {
if(str.startsWith(prefix))
//add to list
else
break;
}
Use a radix tree [wiki] or trie [wiki] if you are concerned about performance.The radix tree is more memory efficient compared to a trie.
Hash maps are only really good at finding exact matches based on some idea of equality which can be appropriately hashed.
Two options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With