Java Collection Tutorial - Java SortedSet.headSet(E toElement)








Syntax

SortedSet.headSet(E toElement) has the following syntax.

SortedSet < E > headSet(E toElement)

Example

In the following code shows how to use SortedSet.headSet(E toElement) method.

/*  ww w.j  av a2s .  c  o m*/
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main {
  public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
  }
}

The code above generates the following result.