Take the 2-minute tour ×

I have a hashtable looking like this:

Hashtable<Mapping, Integer> mappingCount = new Hashtable<Mapping, Integer>();

I want to use this code:

if (mappingCount.get(currentMapping) != null)
    mappingCount.put(currentMapping, mappingCount.get(currentMapping) + 1);
    mappingCount.put(currentMapping, 1);

In order to be able to get the value from the hashtable, for the class Mapping I did the following:

public boolean equals(Object obj) {
    return ((Mapping)obj).mappingXML.equals(this.mappingXML);

However, this doesn't do the trick since mappingCount.get(currentMapping) always results in null. To be sure that something's not wrong, I did the following:

if (aaa.contains(currentMapping.getMappingXML()))
    System.out.println("found it!");

where aaa is List<String> aaa = new ArrayList<String>(). Of course, found it is printed many times. What am I doing wrong?

share|improve this question
Also, don't use Hashtable - it's an obsolescent class with an outdated API. Use HashMap instead. –  Michael Borgwardt May 17 '11 at 14:02
Also equals should not throw ClassCastException or NullPointerException. –  Tom Hawtin - tackline May 17 '11 at 14:06
related: Overriding equals and hashCode in Java –  McDowell May 17 '11 at 14:38
Remember to synchronize HashMap access if multithreaded access required. –  karmakaze May 18 '11 at 1:29

5 Answers 5

up vote 10 down vote accepted

You also need to override the hashCode() method.

From the JavaDocs:

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

The reason for this is that Hashtable uses hashCode as a preliminary test to see if two objects are equals. If the hashCode matches, then it uses equals to check for collissions.

The default implementation of hashCode() returns the memory address of the object, and for two objects that are equal, their hashcodes must also be equal.

Also look at the general contract for hashCode().

share|improve this answer
What should I put for the hashCode? And do I have to do only for classes that I'm using in Hashtable (or HashMap), or for all of them? –  Ivan May 17 '11 at 14:12
Take a look at commons-lang's HashCodeBuilder –  matt b May 17 '11 at 14:20
@Ivan: Eclipse (and most other modern IDEs will generate hashcodes based on your private variables.) You should do it for all objects where you override equals. All the variables that are used in your equals method should be used in your hashcode calculation. –  Reverend Gonzo May 17 '11 at 14:21
@Gonzo: Only variables necessary for equals should be included in hashCode(). Some overly complex equals() may test fields in some branches and not others. –  ILMTitan May 17 '11 at 18:24

Whenever you override equals, you must override hashCode as well.

share|improve this answer

You need to override hashCode as well.

From the Object#hashCode doc:

Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.


share|improve this answer

You have to implement hashcode() as well!


public class Employee{
    int        employeeId;
    String     name;
    Department dept;

    // other methods would be in here 

    public int hashCode() {
        int hash = 1;
        hash = hash * 17 + employeeId;
        hash = hash * 31 + name.hashCode();
        hash = hash * 13 + (dept == null ? 0 : dept.hashCode());
        return hash;
share|improve this answer

All of the recommendations to override equals and hash code correctly are spot on; Joshua Bloch tells you how to do it properly.

But an equally important requirement is that keys in maps must be immutable. If your class can change its values, then the equals and hash code can change after you add it to the map; disaster ensues.

share|improve this answer
+1 Immutability considerations are often overlooked in oo programs. –  karmakaze May 18 '11 at 1:31

Your Answer


