1

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.

Dvora
  • 1,175
  • 1
  • 16
  • 26
  • You need full-text search over the keys. I don't see a way to do that unless you actually iterate and compare the text of all keys. Why is it that you don't want to iterate over the map? – mjuarez Feb 12 '13 at 06:08
  • I doubt that can be done but you could add your Keys to another Hash where the Key could be words and the Value maybe a list of the Keys where it occurs. – vellvisher Feb 12 '13 at 06:12
  • 1
    You need to cut the text up, and map from words --> set of sentences. When you need to check, grab the 2 sets from 2 words and use set intersection. – nhahtdh Feb 12 '13 at 06:17
  • What if there are alot of keys? That's what I want to save avoiding iterating over the map. – Dvora Feb 12 '13 at 06:22
  • @Dvora: Map will get you the key very quickly. The problem is when there are many sentences with the same word. – nhahtdh Feb 12 '13 at 07:02

2 Answers2

2

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...

ATrubka
  • 3,982
  • 5
  • 33
  • 52
0

This may not be possible without iterating as the current Map implementations do not have any such methods. But there are other utility libraries which could help you. Have a look at this question which is similar to the one you have posed. It talks of following two libraries which can help you

Community
  • 1
  • 1
Shiva Kumar
  • 3,111
  • 1
  • 26
  • 34