treeset in java is implemented from code example
Example 1: TreeSet in java
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetExamples
{
public static void main(String[] args)
{
TreeSet<String> ts = new TreeSet<String>();
ts.add("red");
ts.add("blue");
ts.add("green");
ts.add("red");
Iterator<String> iterate = ts.iterator();
while(iterate.hasNext())
{
System.out.println(iterate.next());
}
}
}
Example 2: java treeset
import java.util.*;
Set<String> movies = new TreeSet<String>();
movies.add(E);
movies.add("The Shining");
movies.add("Dawn of the Dead");
movies.add("Zombieland");
Example 3: treeset in java
- TreeSet sorts the order and don't accept null