What collection api to use for mapping a pair of keys to a value?
Giriraj Bhojak
Ranch Hand
Joined: Apr 03, 2008
Posts: 67
posted
0
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.
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
Marshal
Joined: Mar 22, 2005
Posts: 35253
7
posted
0
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
Joined: Apr 03, 2008
Posts: 67
posted
0
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
Marshal
Joined: Mar 22, 2005
Posts: 35253
7
posted
0
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
Joined: Apr 03, 2008
Posts: 67
posted
0
Does this mean that I should have another field in the class Key as hashcode. Something like
Regards, Giriraj.
Krishnamurthy Sivakumar
Greenhorn
Joined: May 29, 2008
Posts: 3
posted
0
Within the hashCode() method, do this: return new String(id+"-"+accessCode).hashCode();
Regards, K.Sivakumar
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
4
posted
0
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
Joined: Oct 13, 2005
Posts: 32708
4
posted
0
Actually you can dispense with the new operator
(id + accessCode).hashCode();
You still need the () however.
Giriraj Bhojak
Ranch Hand
Joined: Apr 03, 2008
Posts: 67
posted
0
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
Marshal
Joined: Mar 22, 2005
Posts: 35253
7
posted
0
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.