HashSet 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 is the HashSet example, translated to use generics:

package examples;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetExample
{
  public static void main(String[] args)
  {
    HashSet<String> set = new HashSet<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);
    }
  }
}

It outputs the following (same as the original):

Vanessa
Joe
Frank
Mary
Sylvia

 

This page was last modified on 27 November, 2007