aspose file tools*
The moose likes Java in General and the fly likes Working with HashMaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Working with HashMaps" Watch "Working with HashMaps" New topic
Author

Working with HashMaps

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18853
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Working with HashMaps