public static void main(String[] args) {
///////////////////////////////////////////////////////////////////////
Person jason1 = new Person(“Jason”, 10);
System.out.println(“jason1 hashCode=” + jason1.hashCode());
Person jason2 = new Person(“Jason”, 10);
System.out.println(“jason2 hashCode=” + jason2.hashCode());
Person jason3 = jason1;
System.out.println(“jason3 hashCode=” + jason3.hashCode());
Person jason4 = new Person(“Jason”, 20);
System.out.println(“jason4 hashCode=” + jason4.hashCode());
if (jason1 == jason2)
System.out.println(“jason1 == jason2”);
else
System.out.println(“jason1 != jason2”); //동일한 reference를 가리키지 않으므로 jason1 != jason2
if (jason1 == jason3)
System.out.println(“jason1 == jason3”); // 동일한 reference이므로 jason1 == jason3
else
System.out.println(“jason1 != jason3”);
if (jason1.equals(jason4))
System.out.println(“jason1 == jason4”);
else
System.out.println(“jason1 != jason4”); //동일한 reference를 가리키지 않으므로 jason1 != jason4
if (jason1.equals(jason2))
System.out.println(“jason1 equals to jason2”); // 내용이 같으므로 jason1 equals to jason2
else
System.out.println(“jason1 is not equal to jason2”);
if (jason1.equals(jason3))
System.out.println(“jason1 equals to jason3”); // 내용이 같으므로 jason1 equals to jason3
else
System.out.println(“jason1 is not equal to jason3”);
if (jason1.equals(jason4))
System.out.println(“jason1 equals jason4”);
else
System.out.println(“jason1 is not equal to jason4”); // 내용이 다르므로 jason1 is not equal to jason4
///////////////////////////////////////////////////////////////////////
Student john1 = new Student(“John”, 10, 100);
Student john2 = new Student(“John”, 10, 100);
Student john3 = john1;
Student john4 = new Student(“John”, 20, 100);
if (john1.equals(john2))
System.out.println(“john1 equals to john2”); // 내용이 같으므로 john1 equals to john2
else
System.out.println(“john1 is not equal to john2”);
if (john1.equals(john3))
System.out.println(“john1 equals to john3”); // 내용이 같으므로 jogn1 equals to john3
else
System.out.println(“john1 is not equal to john3”);
if (john1.equals(john4))
System.out.println(“john1 equals to john4”);
else
System.out.println(“john1 is not equal to john4”); // 내용이 다르므로 john1 is not equal to john4
System.out.println(“HashSet~~~~~~~~~~~~~~~~~~~~~~”); // hashCode가 일치하면 동일한 것으로 간주
Set sList5 = new HashSet();
sList5.add(john1);
sList5.add(john2);
sList5.add(john3);
sList5.add(john4);
sList5.forEach((s) -> System.out.println(s));
System.out.println(“sList5 contains john1 Student(\”John\”, 10, 100): ” + sList5.contains(john1));
System.out.println(“sList5 contains john2 Student(\”John\”, 10, 100): ” + sList5.contains(john2));
System.out.println(“sList5 contains john3 Student(\”John\”, 10, 100): ” + sList5.contains(john3));
System.out.println(“sList5 contains john4 Student(\”John\”, 20, 100):: ” + sList5.contains(john4));
System.out.println(“sList5 contains Student(\”John\”, 20, 100): ” + sList5.contains(new Student(“John”, 20, 100)));
System.out.println(“sList5 contains Student(\”John\”, 20, 200): ” + sList5.contains(new Student(“John”, 20, 200)));
System.out.println(“HashTable~~~~~~~~~~~~~~~~~~~~~~”); // HashTable의 경우 key 값은 hashCode가 일치하면 동일한 것으로 간주하여 replace함
Hashtable<Student, Integer> sList6 = new Hashtable<Student, Integer>();
sList6.put(john1, 1);
sList6.put(john2, 2);
sList6.put(john3, 3);
sList6.put(john4, 4);
sList6.forEach((s, i) -> System.out.println(s + ” ” + i));
}