Home: www.vipan.com Vipan Singla e-mail: vipan@vipan.com
Implementing equals() and hashCode()

Sample Code

Immutable Class

public class TaskToDoImmutable {

    private String taskToDo;
    private long taskTime;

    private int hash;

    public TaskToDoImmutable(String taskToDo, long taskTime) {
        this.taskToDo = taskToDo;
        this.taskTime = taskTime;
        // Object cannot change.  Calculate hash code at creation time.
        this.hash = calculateHashCode();
    }

    public String getTaskToDo() {
        return taskToDo;
    }

    public long getTaskTime() {
        return taskTime;
    }

    public TaskToDoImmutable changeTaskDescription(String newTaskDescription) {
        return new TaskToDoImmutable(newTaskDescription, taskTime);
    }

    public TaskToDoImmutable changeTaskTime(long newTaskTime) {
        return new TaskToDoImmutable(taskToDo, newTaskTime);
    }

    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!this.getClass().equals(obj.getClass())) return false;
        TaskToDoImmutable obj2 = (TaskToDoImmutable) obj;
        if (
            this.taskToDo.equals(obj2.taskToDo) &&
            this.taskTime == obj2.taskTime
        ) {
            return true;
        }
        else return false;
    }

    public int hashCode() {
        return hash;
    }

    private int calculateHashCode() {
        int tmp = 0;
        // Method 1-Concatenate the strings
        tmp = (taskToDo + Long.toString(taskTime)).hashCode();
        // Method 2-Insert a separator before concatenating
        tmp = (taskToDo + "|" + Long.toString(taskTime)).hashCode();
        // Method 3-Add hash codes
        tmp = taskToDo.hashCode() + (new Long(taskTime)).hashCode();
        // Method 4-Multiply with prime and add the results
        tmp = taskToDo.hashCode()*3 + (new Long(taskTime)).hashCode()*5;
        return tmp;
    }

}

Any Class, Especially Mutable Class

import java.util.*;

// This class is shown here as a mutable class
public class TaskToDo {

    private String taskToDo;
    private Date taskTime;

    public TaskToDo(String taskToDo, Date taskTime) {
        this.taskTime = taskTime;
        this.taskToDo = taskToDo;
    }

    public String getTaskToDo() {
        return taskToDo;
    }

    public Date getTaskTime() {
        return taskTime;
    }

    public void setTaskDescription(String newTaskDescription) {
        taskToDo = newTaskDescription;
    }

    public void setTaskTime(Date newTaskTime) {
        taskTime = newTaskTime;
    }

    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!this.getClass().equals(obj.getClass())) return false;
        TaskToDo obj2 = (TaskToDo) obj;
        if (
            this.taskToDo.equals(obj2.taskToDo) &&
            this.taskTime.equals(obj2.taskTime)
        ) {
            return true;
        }
        else return false;
    }

    public int hashCode() {
        return 59878489;
    }

}

Immutable Classes

Implementing equals() and hashCode()

  • Easy part: equals(null) must return false.
  • Difficult part: If firstObject.equals(secondObject) returns true, then secondObject.equals(firstObject) must also return true.
  • Hard part: If equals() returns true for two objects, both objects must return the same integer from the hashCode() method.
  • A particular instance of a class is called an object of that class. All Java classes are subclasses of one class, called java.lang.Object. This class has default implementation of equals() and hashCode() methods which all Java classes inherit. Sometimes in your class, you need to override the default implementation of these two methods.
  • Instantiated objects always have an identity and may also possess state (data fields or instance/class variables) and/or behavior (methods). In Java, an object's identity is based on the class of the object and the memory address in JVM at which it's state is stored.

Implementing equals() - Details

Implementing hashCode() - Details

How A Hashtable Works

Losing Track of Data in a Hashtable

Calculation of Prime Numbers


© Vipan Singla 2002