/** * Title: HashSetPlay * Description: Simple example of using HashSet * @author hollingd@cs.rpi.edu */ import java.util.*; class HashSetPlay { public static void main(String [] args ) { // Create an ArrayList object HashSet x = new HashSet(); // Populate with 10 random Integer objects Random r = new Random(); for (int i=0;i<20;i++) { if (! x.add( new Integer(r.nextInt(100)))) { System.out.println("Duplicate!"); } } System.out.println("Set: " + x); // print all elements of x using iterator Iterator i = x.iterator(); while (i.hasNext()) { int y = ((Integer) i.next()).intValue(); System.out.print( y + " "); } System.out.println(); } }