Add values to TreeSet in Java

Description

The following code shows how to add values to TreeSet.

Example


  /*from   ww  w.  ja  va 2 s . c o  m*/

import java.util.TreeSet;

public class Main {
  public static void main(String args[]) {
    TreeSet<String> ts = new TreeSet<String>();

    ts.add("C");
    ts.add("A");
    ts.add("B");
    ts.add("E");
    ts.add("F");
    ts.add("java2s.com");

    System.out.println(ts);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet