Java Collection Tutorial - Java TreeSet(SortedSet < E > s) Constructor








Syntax

TreeSet(SortedSet < E > s) constructor from TreeSet has the following syntax.

public TreeSet(SortedSet <E> s)

Example

In the following code shows how to use TreeSet.TreeSet(SortedSet < E > s) constructor.

//from ww  w .  java 2  s  . 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));
    TreeSet<String> set1 = new TreeSet<String>(set);
    System.out.println(set1);
  }
}

The code above generates the following result.