Tuesday, July 20, 2010

hashCode() and equals()

with out overriding of hashCode() method.

package com;

class Test{
String r;
Test(String r){
this.r =r;
}
}

public class TestHashCodeAndEquals {

private int no;
private String name;
public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setNo(int no) {
this.no = no;
}

public int getNo() {
return no;
}

public TestHashCodeAndEquals(int no,String name) {
this.setNo(no);
this.setName(name);

}

/*@Override
public int hashCode() {

return 11;
}*/

public static void main(String[] args) {

TestHashCodeAndEquals o1 = new TestHashCodeAndEquals(1,"rama");
System.out.println(o1.hashCode());
TestHashCodeAndEquals o2 = new TestHashCodeAndEquals(1,"rama");
System.out.println(o2.hashCode());
System.out.println(o1.equals(o2));
System.out.println((o1==o2)+" wew");

String s1 = "rama";
String s2 = "rama";
System.out.println(s1==s2);
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
String s11 = new String("rama");
String s21 = new String("rama");
System.out.println(s11==s21);
System.out.println(s11.equals(s21));
System.out.println(s11.hashCode());
System.out.println(s21.hashCode());
Integer i1 = new Integer(10);
Integer i2 = new Integer(1234);
System.out.println(i1==i2);
System.out.println(i1.equals(i2));
System.out.println(i1.hashCode());
System.out.println(i2.hashCode());
Test t1 = new Test("rama");
Test t2 = new Test("rama");
System.out.println(t1.equals(t2));
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());

Thread t = new Thread();
Thread t11 = new Thread(t);
System.out.println(t.hashCode()+" "+t11.hashCode());

// other than System defined wapper classes, we will not get the hashcode equal if and only if we override the hashCode() method in our class.

// even though we override the hashCode() ,override the hashCode() in our class,we can have same hashcode for all instance ,still, equals() will return false

}
}
out put
.......
-1-
4072869
1671711
false
false wew
-2-
true
3492867
3492867
-3-
false
true
3492867
3492867
-4-
false
false
10
1234
-5-
false
4384790
9634993
-6-
1641745 11077203

------------------------------------------------------------------
with overriding of hashCode() method.
package com;

class Test{
String r;
Test(String r){
this.r =r;
}
}

public class TestHashCodeAndEquals {

private int no;
private String name;
public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setNo(int no) {
this.no = no;
}

public int getNo() {
return no;
}

public TestHashCodeAndEquals(int no,String name) {
this.setNo(no);
this.setName(name);

}

@Override
public int hashCode() {

return 11;
}

public static void main(String[] args) {

System.out.println("-1-");
TestHashCodeAndEquals o1 = new TestHashCodeAndEquals(1,"rama");
System.out.println(o1.hashCode());
TestHashCodeAndEquals o2 = new TestHashCodeAndEquals(1,"rama");
System.out.println(o2.hashCode());
System.out.println(o1.equals(o2));
System.out.println((o1==o2)+" wew");

String s1 = "rama";
String s2 = "rama";
System.out.println("-2-");
System.out.println(s1==s2);
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println("-3-");
String s11 = new String("rama");
String s21 = new String("rama");
System.out.println(s11==s21);
System.out.println(s11.equals(s21));
System.out.println(s11.hashCode());
System.out.println(s21.hashCode());
System.out.println("-4-");
Integer i1 = new Integer(10);
Integer i2 = new Integer(1234);
System.out.println(i1==i2);
System.out.println(i1.equals(i2));
System.out.println(i1.hashCode());
System.out.println(i2.hashCode());
System.out.println("-5-");
Test t1 = new Test("rama");
Test t2 = new Test("rama");
System.out.println(t1.equals(t2));
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
System.out.println("-6-");
Thread t22 = new Thread();
Thread t11 = new Thread(t22);
System.out.println(t22.hashCode()+" "+t11.hashCode());

// other than System defined wapper classes, we will not get the hashcode equal if and only if we override the hashCode() method in our class.

// even though we override the hashCode() ,override the hashCode() in our class,we can have same hashcode for all instance ,still, equals() will return false

}
}
out put
.......
-1-
11
11
false
false wew
-2-
true
3492867
3492867
-3-
false
true
3492867
3492867
-4-
false
false
10
1234
-5-
false
1671711
11394033
-6-
4384790 9634993

No comments: