File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Two Way HashMap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two Way HashMap?" Watch "Two Way HashMap?" New topic
Author

Two Way HashMap?

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I want to create an adjacency matrix for a graph as a 2d int array. I am reading in a list of edges consisting of 2 urls so:



for example, represents a link from the index page to the contact page.

At the moment I have created a HashMap<String, Integer> and I create a new entry in the hashmap for each new url that I find and associate it with an integer which will be it's index in the adjacency matrix. So now I can use the url to get the index of that url in constant time. I want to be able to do the reverse as well, ie get the url name by searching a HashMap, which should also be in constant time.

Do I have to make 2 HashMaps for this? It seems a waste of time and memory. Is there a data structure where I can use either side as the key?

Possibly I'm doing this in a bit of a stupid way anyway, but I really want to make the adjacency list as a 2d int array because I can do calculations of pagerank etc quicker that way.

Grateful for any thoughts.

Cheers

Joe


OCPJP 85%
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Joe Lemmer wrote:
Do I have to make 2 HashMaps for this? It seems a waste of time and memory. Is there a data structure where I can use either side as the key?


Check out BidiMap under Apache collections.

These represent maps where the key can lookup the value and the value can lookup the key with equal ease.
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
That looks great. Thanks very much Sridhar.
Shiwakant Bharti
Greenhorn

Joined: Mar 22, 2010
Posts: 2

Basically I too have a requirement for Bi-directional Hash Map. One of the use case is we have is currently in our system one kind of naming say "A" namings, but some clients are interested in another naming say "B". Now for View Screens for another clients we need to map from schema(set) A to B . Alternatively if there is Data Entry Screen then in that case we need to map from B to A so the underlying system is unchanged.

Hopefully this should help. Thanks


Regards,
Shiwakant
----------------
SCJP 6, SCWCD 5, SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two Way HashMap?