This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes What collection api to use for mapping a pair of keys to a value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What collection api to use for mapping a pair of keys to a value?" Watch "What collection api to use for mapping a pair of keys to a value?" New topic
Author

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

Giriraj Bhojak
Ranch Hand

Joined: Apr 03, 2008
Posts: 71
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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
You could either use a map of maps, like

or introduce a class like this for the key:


Ping & DNS - my free Android networking tools app
Giriraj Bhojak
Ranch Hand

Joined: Apr 03, 2008
Posts: 71
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: 41599
    
  55
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: 71
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: 41599
    
  55
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: 71
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
Within the hashCode() method, do this:
return new String(id+"-"+accessCode).hashCode();

Regards,
K.Sivakumar
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
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: 38472
    
  23
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: 71
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: 41599
    
  55
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.
 
jQuery in Action, 2nd edition
 
subject: What collection api to use for mapping a pair of keys to a value?