I have a Map, which its key is a String, that contains the following Keys:
“Good morning to you”
“How are you today”
“This day is butiful”
“Thanks in advance”
I would like to know if there is a key that contains two certain words.
Of course there could be more then one matching key, but I need to know if there is any,a Boolean answer – true or false.
In the case above, for the words “morning” and “you” I would get true, and for “are ” and “butiful” – false.
Is there a way to check such a thing without iterating over the map?
Thanks.
Simple answer: there’s no such way.
There are several types of Map.
HashMap is based on hashing algorithm, which pretty much scrambles string’s contents. It’s done on purpose and there’s no way to reliably derive string’s contents from a hash key.
SortedMap allows you to quickly locate values that are sorted, but can’t help you find words inside those strings.
The only way to do what you need to do is to iterate over entire key set.
Alternatively, you can employ text search algorithms that build index for your set of strings and allow you to speed up the search.
Consider this:
http://lucene.apache.org/core/
Another idea is to come up with your own hashing algorithm or map implementation that fits your need. This could be more complicated than it may seem though…