Higher and lower set

 
import java.util.TreeSet;

public class Main {

  public static void main(String args[]) {
    TreeSet<Integer> ts = new TreeSet<Integer>();

    ts.add(8);
    ts.add(19);
    ts.add(-2);
    ts.add(3);

    System.out.println(ts);

    System.out.println("First element in ts: " + ts.first());
    System.out.println("Last element in ts: " + ts.last());

    System.out.println("First element > 15: " + ts.higher(15));
    System.out.println("First element < 15: " + ts.lower(15));
  }
}
  

Output:

 
[-2, 3, 8, 19]
First element in ts: -2
Last element in ts: 19
First element > 15: 19
First element < 15: 8
  
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