• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Two Way HashMap?

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 317
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks great. Thanks very much Sridhar.
 
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic