Java Collection Tutorial - Java TreeSet() Constructor








Syntax

TreeSet() constructor from TreeSet has the following syntax.

public TreeSet()

Example

In the following code shows how to use TreeSet.TreeSet() constructor.

 //  w w w  .  j  a va2  s  .  co  m
import java.util.Set;
import java.util.TreeSet;

public class Main {
  public static void main(String[] args) {
    Set<String> ss = new TreeSet<String>();
    String[] fruits = { "apples", "pears", "grapes", "bananas", "kiwis" };
    for (String fruit : fruits){
      ss.add(fruit);
    }
    for (String s : ss){
      System.out.print(s);
    }
  }
}
  

Output: