Java Collection Tutorial - Java TreeSet(Collection <? extends E > c) Constructor








Syntax

TreeSet(Collection <? extends E > c) constructor from TreeSet has the following syntax.

public TreeSet(Collection <? extends E> c)

Example

In the following code shows how to use TreeSet.TreeSet(Collection <? extends E > c) constructor.

/*from  w  ww .  j  a  v  a 2s .c om*/
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.headSet("D"));
    System.out.println(set.tailSet(""));
  }
}
  

The output: