• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to use an object-id in hashmap

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


override hashCode method
 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaranch.com/journal/2002/10/equalhash.html
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston, its just an example, so i just need to know the proper way to use composite keys in hashmap
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic