Java Collection Tutorial - Java SortedSet.subSet(E fromElement, E toElement)








Syntax

SortedSet.subSet(E fromElement, E toElement) has the following syntax.

SortedSet < E > subSet(E fromElement,   E toElement)

Example

In the following code shows how to use SortedSet.subSet(E fromElement, E toElement) method.

import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
//www .ja v a  2s .  c om
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.