This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two Way HashMap?

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks great. Thanks very much Sridhar.
 
Shiwakant Bharti
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic