Since a TreeSet is ordered, its subset is also ordered. The two that are simplest to explain are headset() and tailSet():
public SortedSet headSet(Object toElement) public SortedSet tailSet(Object fromElement)
import java.util.Arrays; import java.util.TreeSet; public class MainClass { public static void main(String args[]) throws Exception { String elements[] = { "A", "C", "D", "G", "F" }; TreeSet set = new TreeSet(Arrays.asList(elements)); System.out.println(set.headSet("D")); System.out.println(set.tailSet("")); } }
[A, C] [A, C, D, F, G]