In the following code, we create two different TreeSet objects.
We assign some values to the first object, and then assign a subset of the 1st object to the second object. Then we add the element (609) to the 1st object only . Why then does this new element appear in both objects?
import java.util.*;
public class Explorer1 {
public static void main(String[] args) {
TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
subs = (TreeSet)s.subSet(608, true, 611, true);
s.add(609);
System.out.println(s + " " + subs);
}
}
Output: [606, 608, 609, 610, 612] [608, 609, 610]
source
share