The first and last element in this TreeSet

E first()
Returns the first (lowest) element currently in this set.
E last()
Returns the last (highest) element currently in this set.

  import java.util.Arrays;
import java.util.TreeSet;

public class Main {
  public static void main(String args[]) throws Exception {
    String elements[] = { "java2s.com", "C", "D", "G", "F" };
    TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));
    System.out.println(set);
    System.out.println(set.first());
    System.out.println(set.last());
  }
}
  

The output:


[C, D, F, G, java2s.com]
C
java2s.com
Home 
  Java Book 
    Collection  

TreeSet:
  1. TreeSet class
  2. Create TreeSet objects
  3. Add elements to a TreeSet
  4. Get the least and greatest element in this TreeSet'>
  5. The first and last element in this TreeSet
  6. Get the head set and tail set
  7. Remove element from TreeSet
  8. Get the size of this TreeSet
  9. Get a subset from this TreeSet