Java Collection How to - Add value to a sorted set








Question

We would like to know how to add value to a sorted set.

Answer

 // w  w  w.  j  av a2  s .  c  o m


import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main {
  public static void main(String[] argv) throws Exception {
    SortedSet<String> set = new TreeSet<String>();
    set.add("b");
    set.add("c");
    set.add("a");

    Iterator it = set.iterator();
    while (it.hasNext()) {
      Object element = it.next();
    }
    String[] array = (String[]) set.toArray(new String[set.size()]);
  }
}