Get a subset from this TreeSet

ReturnMethodSummary
NavigableSet<E>subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)Returns a view of the portion of this set whose elements range from fromElement to toElement.
SortedSet<E>subSet(E fromElement, E toElement)Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

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

public class Main {
  public static void main(String args[]) throws Exception {

    String elements[] = { "java2s.cm", "C", "D", "G", "F" };
    TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));

    System.out.println(set.subSet("C", "F"));
  }
}

The output:


[C, D]
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.