• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

What collection api to use for mapping a pair of keys to a value?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a requirement where I need to fetch data from database
Once I fetch it I also need to insert it into a map(a hashmap) for later retrieval.
For a query on only one filter let's say "id" this approach is alright.
I can easily fetch data filtered on "id" and then store it into hashmap where key value pair is <id,data>.

But now I have a requirement wherein i need to fetch data using id and access code.
Then I also need to insert it into a hashmap for faster retrieval next time.
Something like <(id,access code), data>.
How do I do this?
Which data structure would help me do this?
I hope i have made my requirement clear enough.

Regards,
Giriraj.
[ December 19, 2008: Message edited by: Giriraj Bhojak ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could either use a map of maps, like

or introduce a class like this for the key:
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
You could either use a map of maps, like

or introduce a class like this for the key:



Thanks a lot Ulf for your speedy reply...

Regards,
Giriraj.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.

If you end up using a custom key class, be sure to override its "equals" method (so that it returns "true" if key and access code are the same) and also the "hashCode" method. Otherwise you may see strange behavior.
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding equals method should not be a problem I guess.
I would just need to compare the values of id and accessCode which are of Strings datatype.
But what would be the best way to override hashcode since both my operands are Strings(id and accessCode)

Regards,
Giriraj.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think something like this should do

Note that you can calculate this once during object instantiation; no need to calculate it over and over again.
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this mean that I should have another field in the class Key as hashcode.
Something like


Regards,
Giriraj.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Within the hashCode() method, do this:
return new String(id+"-"+accessCode).hashCode();

Regards,
K.Sivakumar
 
Marshal
Posts: 79971
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Giriraj Bhojak:
Does this mean that I should have another field in the class Key as hashcode.



I think that is what Ulf meant, yes. Probably best to change

to

Then you can be confident those values won't change.

And welcome to JavaRanch ( ), Krishnamurthy Sivakumar.
 
Campbell Ritchie
Marshal
Posts: 79971
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually you can dispense with the new operator

(id + accessCode).hashCode();

You still need the () however.
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you all for helping me with this.

If I introduce another field named hashCode, how do I make sure that this value is being used(as key)for retrieval of data from hashmap during call to map.get(key).

Regards,
Giriraj
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If I introduce another field named hashCode, how do I make sure that this value is being used(as key)for retrieval of data from hashmap during call to map.get(key).


Just to be picky, but hashCode is not the key. The object instance of the Key class (or whatever you end up naming it) is the key.

You'd write the hashCode method like

That's it.
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic