Wednesday, 14 August 2013

Java Collections que

Java Collections que

import java.util.*;
class KeyMaster {
public int i;
public KeyMaster(int i) { this.i = i; }
public boolean equals(Object o) { return i == ((KeyMaster)o).i; }
public int hashCode() { return i; }
}
public class MapIt {
public static void main(String[] args) {
Set<KeyMaster> set = new HashSet<KeyMaster>();
KeyMaster k1 = new KeyMaster(1);
KeyMaster k2 = new KeyMaster(2);
set.add(k1); set.add(k1);
set.add(k2); set.add(k2);
System.out.print(set.size() + ":");
k2.i = 1;
System.out.print(set.size() + ":");
set.remove(k1);
System.out.print(set.size() + ":");
set.remove(k2);
System.out.print(set.size());
}
}
What is the result?
A. 4:4:2:2
C. 2:2:1:0
E. 2:1:0:0
G. 4:3:2:1
B. 4:4:3:2
D. 2:2:0:0
F. 2:2:1:1
Answer: F
Can anybody explain the answer. My doubt is this. K2's i changed but set
still has 2 elements one of which i think still refers to the object
referred by changed k2. so why doesnt remove(k2) works?

No comments:

Post a Comment