Java - equals() and hashcode() Implementation

The below code sample provides a basic implementation of equals() and hashcode() methods of Object class in Java. Consider a class named Device, with two attributes deviceId and deviceName. We assume that for two device objects to be equal, both their deviceId and deviceName should be the same. We have used the same attribute to calculate hashcode also. It takes each character's ASCII value in the deviceName and multiplies it with its position. Finally the resultant value is added to deviceId. In this way hashcode is calculated with both the attributes. Now, any object of this Device class can be used as a key for a Hash Map.

import java.io.Serializable;

public class Device implements Serializable
{
        private static final long       serialVersionUID        = 597177821000985679L;

        private int                                     deviceId;

        private String                          deviceName;

        public int getDeviceId()
        {
                return deviceId;
        }

        public void setDeviceId(int deviceId)
        {
                this.deviceId = deviceId;
        }

        public String getDeviceName()
        {
                return deviceName;
        }

        public void setDeviceName(String deviceName)
        {
                this.deviceName = deviceName;
        }

        public Device()
        {
        }

        @Override
        public int hashCode()
        {
                int s = 0;
                for (int i = 0; i < deviceName.length(); i++)
                {
                        s += (int) deviceName.charAt(i) * i;
                }
                return s + deviceId;
        }

        @Override
        public boolean equals(Object obj)
        {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                final Device dev = (Device) obj;
                if (this.getDeviceId() == 0)
                {
                        if (dev.getDeviceId() != 0)
                                return false;
                }
                else if (!(this.getDeviceId() == dev.getDeviceId()))
                        return false;
                if (this.getDeviceName() == null)
                {
                        if (dev.getDeviceName() != null)
                                return false;
                }
                else if (!this.getDeviceName().equalsIgnoreCase(dev.getDeviceName()))
                {
                        return false;
                }
                return true;
        }

        public static void main(String[] args)
        {
                Device dev1 = new Device();
                dev1.setDeviceId(1001);
                dev1.setDeviceName("CSPSeries");
       
                Device dev2 = new Device();
                dev2.setDeviceId(1001);
                dev2.setDeviceName("CSPSeries");
               
                System.out.println("Created two devices 'dev1' and 'dev2' with same deviceId and deviceName. Are they equal? "
                                + dev1.equals(dev2));
               
                System.out.println("Modify the value of deviceId for 'dev2' . ");
               
                dev2.setDeviceId(2011);
               
                System.out.println("Now Compare 'dev1' and 'dev2' Are they equal? " + dev1.equals(dev2));
       
                System.out.println("Create a new device 'dev3' with deviceId and deviceName as null ");
               
                Device dev3 = new Device();
                System.out.println("Is dev3 equals dev2 -> " + dev3.equals(dev2));
        }
}

Technology: 

Search