Home > Uncategorized > HashCode and Equals

HashCode and Equals

HashCode, HashSet, HashTable,  java.lang.Object.equals(), java.lang.Object.hashCode() – are related to each other

Below is a sample code to prove that.

findSize() is a method, which accept class whose constructor accepts an Integer. This method will create 5 object and push into HashSet, finally will print Set’s size. It should be 5, if all each object is unique. Output should be 1, if all are having same content.

Case – One (If hashCode is equal, doesn’t mean objects are equal)

I have Customer object, regardless of content, it’s hashCode will remain same. When executing findSize method with Cusomer.class as argument, Set contains 5 unique object.

It proves that even if content and hashCode is equal, hashSet can’t understand unless we override equals method.

Case – Two (If hashCode is unique, doesn’t mean objects are unique)

I have Customer2 object, whose hashCode was not overriden, hence it would be unique (provided by JVM). Its equals method is overriden. When two Customer2 object has same name, they are considered equal.

But when invoking findSize, even Customer2 will have same effect as Case-One.  so even if we override equals without overriding hashCode, They are considered as 5 different Object as their hasCode is unique.

Case – Three (If you override equals, override hashCode )

Now we have Customer3 object, whose hashCode,equals methods have overridden.  Here when invoking findSize with argument as Customer3.class, now it recognize that all the Objects are same.

Case – Four (Object hashCode, and its Clone hashCode will not match)

I have a method, which creates Customer4 object, it clones it and checks if original and cloned objects are equal, But output was false. This ensures that Cloned object will not contain same hashCode as its original.

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashSet;

public class HashTest {
public static void main(String args[]) throws InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchMethodException,
InvocationTargetException {
findSize(Customer.class);
findSize(Customer2.class);
findSize(Customer3.class);
findSize2(Customer3.class);
proveHashcodeIsUnique();
}

private static void findSize(Class<? extends Obj> clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException {
HashSet<? super Obj> set = (HashSet<? super Obj>) new HashSet(5);
for (int i = 0; i < 5; i++) {
Constructor<Obj> constructor = (Constructor<Obj>) clazz.getConstructor(Integer.class);
set.add(constructor.newInstance(Arrays.asList(1).toArray()));
}
System.out.println(set.size());
}

private static void findSize2(Class<? extends Obj> clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException {
HashSet<? super Obj> set = (HashSet<? super Obj>) new HashSet(5);
for (int i = 0; i < 5; i++) {
Constructor<Obj> constructor = (Constructor<Obj>) clazz.getConstructor(Integer.class);
set.add(constructor.newInstance(Arrays.asList(i).toArray()));
}
System.out.println(set.size());
}

private static void proveHashcodeIsUnique() {
Customer4 cus1 = new Customer4(1);
Customer4 cus2 = cus1.clone();
System.out.println(cus1.hashCode() == cus2.hashCode());
}

}

interface Obj {
}

class Customer implements Obj {
private String name;

public Customer(Integer i) {
name = i + "";
}

@Override
public int hashCode() {
return 17;
}
}

class Customer2 implements Obj {
private String name;

public Customer2(Integer i) {
name = i + "";
}

public boolean equals(Object other) {
Customer2 c1 = (Customer2) other;
return c1.name.equals(this.name);
}

}

class Customer3 implements Obj {
private String name;

public Customer3(Integer i) {
name = i + "";
}

public boolean equals(Object other) {
Customer3 c1 = (Customer3) other;
return c1.name.equals(this.name);
}

public int hashCode() {
return name.hashCode();
}

}

class Customer4 implements Cloneable, Obj {
private String name;

public Customer4(Integer i) {
name = i + "";
}

public boolean equals(Object other) {
Customer4 c1 = (Customer4) other;
return c1.name.equals(this.name);
}

@Override
public Customer4 clone() {
Customer4 result = null;
try {
result = (Customer4) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return result;
}
}
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: