You are on page 1of 2

import java.util.

*;
class Employee {
private String id;
private String name;
private double salary;
Employee() {}
Employee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String toString() {
return "[" + "id=" + id + ",name=" + name + ",salary=" + salary + "]";
}
public boolean equals(Object o) {
System.out.println("equals()...");
boolean result = false;
if (this == o) {
// System.out.println("line#24");
result = true;
} else {
// System.out.println("line#27");
if (o instanceof Employee) {
// System.out.println("line#29");
Employee oEmp = (Employee) o;
if (this.id == oEmp.id && this.name == oEmp.name
&& this.salary == oEmp.salary) {
// System.out.println("line#34");
result = true;
}
} else {
// System.out.println("line#38");
result = false;
}
}
return result;
}
public int hashCode() {
System.out.println("hashCode()...");
int result = this.id.hashCode() + this.name.hashCode()
+ (int) System.currentTimeMillis();
return result;
}
}
class CollectionsDemo {
public static void main(String[] args) {
System.out.println("Hello World!");
Collection cSet = new HashSet();
Collection cList = new ArrayList();
Employee e1, e2, e3, e4, e5, e8;
Employee e6 = null;
Employee e7 = null;
e1 = new Employee("1", "Anand", 11500);
e5 = new Employee("1", "Anand", 11500);
e8 = new Employee("1", "Anand", 11500);
e2 = new Employee("2", "Anand", 1500);
e3 = new Employee("3", "Vibhore", 30500);
e4 = e1;
// System.out.println("e1:" + e1.hashCode());
// System.out.println("e2:" + e2.hashCode());
// System.out.println(e3.hashCode());
// System.out.println("e4:" + e4.hashCode());
// System.out.println("e5:" + e5.hashCode());
// System.out.println(e6.hashCode());
// System.out.println(e7.hashCode());
// System.out.println(e8.hashCode());
// System.out.println(e1.toString() + e2.toString() + e3.toString());
// System.out.println(e1.equals(e2));
// System.out.println(e1.equals(e4));
// System.out.println("e1==e5?" + e1.equals(e5));
// System.out.println("e1==e7?" + e1.equals(e7));
// System.out.println("Reflective:e1==e1?" + e1.equals(e1));
// System.out.println("Symmetric:e1==e5?" + e1.equals(e5));
// System.out.println(
// "Transitive:e1==e5 and e5==e8?" + e1.equals(e5) + e5.equals(e8));
System.out.println("Adding e1:"+cSet.add(e1));
System.out.println("Adding e2:"+cSet.add(e2));
// System.out.println(cSet.add(e3));
System.out.println("Adding e4:"+cSet.add(e4));
System.out.println("Adding e5:"+cSet.add(e5));
// System.out.println(cSet.add(e6));
// System.out.println(cSet.add(e7));
// System.out.println(cSet.add(e8));
// cList.add(e1);
// cList.add(e2);
// cList.add(e3);
// cList.add(e4);
// cList.add(e5);
// cList.add(e6);
// cList.add(e7);
// cList.add(e8);
System.out.println("cSet:" + cSet);
System.out.println("size:" + cSet.size());
//System.out.println("cList:" + cList + ",size:" + cList.size());
}
}

You might also like