aspose file tools*
The moose likes Java in General and the fly likes Retrieval of Key through Values in a Map. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Retrieval of Key through Values in a Map." Watch "Retrieval of Key through Values in a Map." New topic
Author

Retrieval of Key through Values in a Map.

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use multiple maps, with keys appropriate to each search.


[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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".


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Jul 11, 2001
Posts: 14112
Take a look at http://jakarta.apache.org/commons/collections/api-3.1/org/apache/commons/collections/BidiMap.html


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieval of Key through Values in a Map.