Explain Mutable Hash Keys in HashSet and ArrayList
Company: Millennium
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
A Java `Point` has mutable integer fields x and y, value-based `equals`, and `hashCode()` implemented with `Objects.hash(x, y)`. Its `toString()` returns `(x, y)`. Execute:
```java
Collection<Point> set = new HashSet<>();
Collection<Point> list = new ArrayList<>();
Point p = new Point(1, 1);
set.add(p);
list.add(p);
System.out.println(set.contains(p));
System.out.println(list.contains(p));
p.x = 2;
Point p2 = new Point(2, 1);
System.out.println(set.contains(p2));
System.out.println(set.contains(p));
System.out.println(list.contains(p));
set.remove(p);
System.out.println(set);
```
Explain the observed behavior in a conventional Java HashSet implementation and how to prevent the broken lookup.
### Clarifying Questions to Ask
- Which fields participate in equality and hashing, and can they change while an object belongs to a set?
- Is object identity or coordinate value the intended membership rule?
```hint Stored location versus current value
Changing coordinates does not cause the set to remove and reinsert the existing entry.
```
### What a Strong Answer Covers
- Why lookup uses the new hash while the entry was indexed under its original hash.
- Why ArrayList lookup still succeeds.
- Why removal can fail and leave the mutated object visible during iteration.
- An immutable-key or remove-before-mutate repair.
### Follow-up Questions
- Why does calling remove after mutation fail to repair the set?
- What happens if two set members become equal after mutation?
Overview: Analyze mutable Java HashSet keys, failed contains and remove calls, ArrayList behavior, and immutable-key designs that preserve equality and hashing.
A Java Point has mutable integer fields x and y, value-based equals, and hashCode() implemented with Objects.hash(x, y). Its toString() returns (x, y). Execute:
Collection<Point> set = new HashSet<>();
Collection<Point> list = new ArrayList<>();
Point p = new Point(1, 1);
set.add(p);
list.add(p);
System.out.println(set.contains(p));
System.out.println(list.contains(p));
p.x = 2;
Point p2 = new Point(2, 1);
System.out.println(set.contains(p2));
System.out.println(set.contains(p));
System.out.println(list.contains(p));
set.remove(p);
System.out.println(set);
Explain the observed behavior in a conventional Java HashSet implementation and how to prevent the broken lookup.
Clarifying Questions to Ask Guidance
Which fields participate in equality and hashing, and can they change while an object belongs to a set?
Is object identity or coordinate value the intended membership rule?
What a Strong Answer Covers Guidance
Why lookup uses the new hash while the entry was indexed under its original hash.
Why ArrayList lookup still succeeds.
Why removal can fail and leave the mutated object visible during iteration.
An immutable-key or remove-before-mutate repair.
Follow-up Questions Guidance
Why does calling remove after mutation fail to repair the set?
What happens if two set members become equal after mutation?