Given:
import java.util.TreeSet; public class Main { public static void main(String[] args) { TreeSet<String> t1 = new TreeSet<String>(); TreeSet<String> t2 = new TreeSet<String>(); t1.add("b"); t1.add("7"); t2 = (TreeSet) t1.subSet("5", "c"); try {/*from w ww . j a v a 2 s . c o m*/ t1.add("d"); t2.add("6"); t2.add("3"); } catch (Exception e) { System.out.print("ex "); } System.out.println(t1 + " " + t2); } }
What is the result?
D is correct.
The subSet()
method is creating a "backed" collection with a certain value range.
Additions to either collection that are in the backed collection's range will be added to both.
Attempting to add an out-of-range entry to the backed collection throws an exception.