• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Working with HashMaps

 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a class in which I need to work with a HashMap. I've read through the Java Collections Trail, but I still have a couple of questions.

In Eclipse, I get this warning when I try to use the put() method of my HashMap.

"Type safety: The method put(Object, Object) belongs to the raw type
HashMap. References to generic type HashMap<K,V> should be
parameterized"

Does this have something to do with Generics in Java 1.5? If this is the case, can I just pass two objects to the put() method if I'm compiling for Java 1.4?

Also, If I just pass two custom Objects to the HashMap, does Java know how to hash the custom object passed as a key? How does it do this? Does it hash the "binary" representation of the object? (I don't think that would require no previous knowledge of the custom objects stucture.)Or are you only allowed to pass certain types of objects as keys to a HashMap?

Thanks for the help.

Landon
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Landon,

Yes, the warning means you're ignoring the Generics feature of Java 5. You can set Eclipse to not give this warning if you're using Java 1.4 (sorry, I don't have a copy running or I'd tell you exactly how.)

The hash code for the key is computed entirely and solely using the return value of the key's hashCode() method. Objects that have hashCode() methods that return different values over time make bad keys, as they'll corrupt the HashMap if the hash code changes while the object is in the Map.
 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this have something to do with Generics in Java 1.5? If this is the case, can I just pass two objects to the put() method if I'm compiling for Java 1.4?


Yes... it is a Java 5.0 issue. Since you are using Eclipse (and you want to work in Java 1.4), you can quickly fix this by changing ... Windows --> Preferences --> Java --> Compiler --> Compliance Level --> to Java 1.4. For the longer term, you should also change the JDK that eclipse is using to 1.4 too... Windows --> Perferences --> Java --> Installed JREs.

Also, If I just pass two custom Objects to the HashMap, does Java know how to hash the custom object passed as a key? How does it do this? Does it hash the "binary" representation of the object? (I don't think that would require no previous knowledge of the custom objects stucture.)Or are you only allowed to pass certain types of objects as keys to a HashMap?


The Hashmap hashes the key, by calling the hashCode() method. If you have a custom object, you should consider overriding the equals() and hashCode() methods.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic