Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieval of Key through Values in a Map.

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am doing a simple application in which I am using a LinkedHashMap implementation of Map to store <contactName> and <contactNumber> pairs.

and i am implementing two kinds of search searchByName() and searchByNumber();

while searching by Name i can easily retrieve the results where as while searching through the number there is no direct way to do that according to what i know so far. So i used the following approach.

If anybody knows some other efficient way for doing the same please let me know.

Thanks in Advance!!!
dinesh.

[ Added code tags - Jim ]
[ January 01, 2007: Message edited by: Jim Yingst ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use multiple maps, with keys appropriate to each search.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, if you know that no key is ever the same as any value, then you can use just one map, with one forward and one backward mapping per key-value pair. In the case of telephone numbers and people's names, you're pretty safe; there's unlikely to be a "Mr 990-12345" or anyone whose number is "Jane Smith".
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is interesting peter. I never thought of that. I like it.

To the OP, I don't think your technique even works. The valuset and the key set I do not think are guaranteed to be returned in the same order, nor are they guaranteed to iterate in the same order. I don't think.

Try using entry set.

There is another dirty approach like so;

[ January 02, 2007: Message edited by: Mr. C Lamont Gilbert ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at http://jakarta.apache.org/commons/collections/api-3.1/org/apache/commons/collections/BidiMap.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic