Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Does this mean that I should have another field in the class Key as hashcode.
Something like


Regards,
Giriraj.
 
Krishnamurthy Sivakumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within the hashCode() method, do this:
return new String(id+"-"+accessCode).hashCode();

Regards,
K.Sivakumar
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic