TreeSet Example
Home ] Up ] Vector/Enumeration Example ] ArrayList/Iterator Example ] LinkedList Example ] Stack Example ] Comparable Example ] Comparator Example ] HashSet Example ] [ TreeSet Example ] TreeSet with Comparator ] HashMap Example ] TreeMap Example ] TreeMap with Comparator ]

 

 

Here's the TreeSet example, translated to using generics:

package examples;

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

public class TreeSetExample
{
  public static void main(String[] args)
  {
    TreeSet<String> set = new TreeSet<String>();
    set.add("Mary");
    set.add("Frank");
    set.add("Joe");
    set.add("Sylvia");
    set.add("Vanessa");
    set.add("Frank");   // Duplicate

    for (Iterator<String> iter = set.iterator(); iter.hasNext(); )
    {
      String name = iter.next(); // Note NO required cast
      System.out.println(name);
    }
  }
}

Here's the output of the example (same as the original):

Frank
Joe
Mary
Sylvia
Vanessa

This page was last modified on 27 November, 2007