-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualsHashCodeDemo.java
More file actions
120 lines (95 loc) · 4.2 KB
/
Copy pathEqualsHashCodeDemo.java
File metadata and controls
120 lines (95 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.*;
/**
* Demonstrates the proper contract for equals() and hashCode()
* Key concepts: Contract rules, HashMap behavior, common pitfalls
*/
public class EqualsHashCodeDemo {
public static void main(String[] args) {
System.out.println("=== Equals and HashCode Contract Demo ===\n");
demonstrateProperImplementation();
demonstrateBrokenContract();
demonstrateHashMapBehavior();
}
private static void demonstrateProperImplementation() {
System.out.println("1. Proper Implementation");
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Alice", 25);
Person p3 = new Person("Bob", 30);
System.out.println(" p1.equals(p2): " + p1.equals(p2) + " (same content)");
System.out.println(" p1.equals(p3): " + p1.equals(p3) + " (different content)");
System.out.println(" p1.hashCode(): " + p1.hashCode());
System.out.println(" p2.hashCode(): " + p2.hashCode() + " (same as p1)");
System.out.println(" p3.hashCode(): " + p3.hashCode() + " (different)\n");
}
private static void demonstrateBrokenContract() {
System.out.println("2. Broken Contract Example");
BrokenPerson bp1 = new BrokenPerson("Charlie", 35);
BrokenPerson bp2 = new BrokenPerson("Charlie", 35);
System.out.println(" bp1.equals(bp2): " + bp1.equals(bp2) + " (overridden equals)");
System.out.println(" bp1.hashCode(): " + bp1.hashCode());
System.out.println(" bp2.hashCode(): " + bp2.hashCode() + " (different! BROKEN)");
System.out.println(" ⚠️ Contract violated: equal objects must have same hashCode\n");
}
private static void demonstrateHashMapBehavior() {
System.out.println("3. Impact on HashMap");
// Proper implementation
Map<Person, String> properMap = new HashMap<>();
Person key1 = new Person("David", 40);
properMap.put(key1, "Engineer");
Person key2 = new Person("David", 40); // Equal to key1
System.out.println(" Proper implementation:");
System.out.println(" Map contains key2? " + properMap.containsKey(key2) + " ✓");
System.out.println(" Retrieved value: " + properMap.get(key2));
// Broken implementation
Map<BrokenPerson, String> brokenMap = new HashMap<>();
BrokenPerson bkey1 = new BrokenPerson("Eve", 45);
brokenMap.put(bkey1, "Manager");
BrokenPerson bkey2 = new BrokenPerson("Eve", 45); // Equal to bkey1
System.out.println("\n Broken implementation:");
System.out.println(" Map contains bkey2? " + brokenMap.containsKey(bkey2) + " ✗");
System.out.println(" Retrieved value: " + brokenMap.get(bkey2) + " (null!)");
System.out.println(" ⚠️ HashMap lookup fails due to broken contract\n");
}
}
// Proper implementation
class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
// Broken implementation - equals without hashCode
class BrokenPerson {
private final String name;
private final int age;
public BrokenPerson(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BrokenPerson that = (BrokenPerson) o;
return age == that.age && Objects.equals(name, that.name);
}
// Missing hashCode() - uses Object's default implementation
// This violates the contract!
}