This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to use an object-id in hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to use an object-id in hashmap" Watch "how to use an object-id in hashmap" New topic
Author

how to use an object-id in hashmap

Shazia Bashir
Greenhorn

Joined: Jun 11, 2012
Posts: 11
Hi,

i have this code stated below. I want to save all objects of type CarOwner in an Hashmap. The unique id for CarOwner-object is the car.id and person.id together. What is the best practice to populate the hashmap with this composite key ?

Thanks. Hope you understand my question.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Shazia Bashir wrote:What is the best practice to populate the hashmap with this composite key

override hashCode method
Shazia Bashir
Greenhorn

Joined: Jun 11, 2012
Posts: 11
so in CarOwner class i override the hashCode method, this the right way? code below. the only thing im not sure about when using hashcode, is that it returns an int . Lets say we want to save million and millions in this hashmap, will the hashCode be able to support that ?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.javaranch.com/journal/2002/10/equalhash.html
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4341
    
    7

I think there's some confusion here.

You don't need to override hashCode() for the object being used as the value. You override it for the object being used as the key.

So I think there are at least three possibilities.

1. Do what you're doing in your first code. But I'd suggest tidying it up slightly. Firstly, put a delimiter between the Owner and Car id. Secondly, you might want to create a method to make the composite Id.

2. Create a class to be used for the key: e.g. CarOwnerKey, containing a Car id and an Owner id. Override equals() and hashCode() in this class.

3. Use a different data structure (the best structure would depend on your use cases). E.g. Map<String, Map<String, CarOwner>>, using the Car id for the first key and the Owner id for the second.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7501
    
  18

Shazia Bashir wrote:I want to save all objects of type CarOwner in an Hashmap. The unique id for CarOwner-object is the car.id and person.id together. What is the best practice to populate the hashmap with this composite key ?

Slightly off-topic, but why are you combining the two ID's? Surely a CarOwner is a person? At least using the person ID would stop the same owner being added twice.

Of course, your "owners" could own more than one car and this might be intersection data (ie, all combinations of cars and people); however, this probably only makes sense if your "Car" is a make (or model) of Car. An individual car is only likely to have one owner: a Person.

If you need to allow for company-owned cars, just add a prefix ('C' or 'P' ?) to the ID that says what type of owner you're dealing with, but either way, I suspect you'll find that your "owner" table is simply a list of the entities that own a car on your system.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Shazia Bashir
Greenhorn

Joined: Jun 11, 2012
Posts: 11
Hi Winston, its just an example, so i just need to know the proper way to use composite keys in hashmap
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7501
    
  18

Shazia Bashir wrote:Hi Winston, its just an example, so i just need to know the proper way to use composite keys in hashmap

Pretty much the same way as you'd specify any other key. My suggestion would be to simply create a CompositeKey class that takes any number of fields as objects, and hashes them at construction time using the objects' own hashCode()s. If you use a vararg, you could also use Arrays.equals() to determine if two keys are the same (maybe only after checking if their hashcodes are the same).

Then, add a getKey() method to your CarOwner class that simply returns whatever you want as your composite key (just remember to return the same one every time it's called).

There's quite a lot to know about writing good hashcode routines; but fortunately there's also plenty of material out there. You might want to start by reading the advice in Effective Java; it's never steered me wrong yet.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use an object-id in hashmap
 
Similar Threads
Can one object access a private variable of another object of the same class?
EL Mystery
HashMap Help?!
Using final
hashCode and equals implementation