First in a TreeSet


import java.util.TreeSet;

public class TreeSetDemo {
  public static void main(String[] argv) {
    TreeSet tm = new TreeSet(String.CASE_INSENSITIVE_ORDER);
    tm.add("Gosling");
    tm.add("da Vinci");
    tm.add("van Gogh");
    tm.add("Java To Go");
    tm.add("Vanguard");
    tm.add("Darwin");
    tm.add("Darwin"); // TreeSet is Set, ignores duplicates.

    System.out.println("Lowest (alphabetically) is " + tm.first());
    System.out.println(tm.tailSet("k").toArray().length+ " elements higher than \"k\"");

    System.out.println("Sorted list:");
    java.util.Iterator t = tm.iterator();
    while (t.hasNext()){
      System.out.println(t.next());
    }
  }
}
Home 
  Java Book 
    Runnable examples  

Collection TreeSet:
  1. TreeSet is a sorted set(SortedSet)
  2. First in a TreeSet
  3. Head Set from TreeSet
  4. Higher and lower set
  5. Last from a Set
  6. Reverse order a TreeSet
  7. Tail set, header set and sub set