import java.awt.*; import java.util.*; /** * This example program fails to find the Point on the second call * because you're not supposed to mutate a hash collection's element/key * after you've added it. Or, more specifically, the mutation will change * its hashCode result, which changes the hash bucket Java looks in to find * the value later. * @author Marty Stepp * @version CSE 331 Spring 2011, 5/25/2011 */ public class Example1 { public static void main(String[] args) { Point p = new Point(3, 4); Set set = new HashSet(); set.add(p); System.out.println(set.contains(new Point(3, 4))); // true p.translate(2, 2); System.out.println(set.contains(new Point(5, 6))); // false Point p2 = new Point(5, 6); set.add(p2); } }