This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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 ?
Shazia Bashir wrote:What is the best practice to populate the hashmap with this composite key
override hashCode method
Joined: Jun 11, 2012
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 ?
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.
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.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Joined: Jun 11, 2012
Hi Winston, its just an example, so i just need to know the proper way to use composite keys in hashmap
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.