01: import java.util.HashSet;
02: import java.util.Iterator;
03: import java.util.Set;
04: import javax.swing.JOptionPane;
05: 
06: /**
07:    This program demonstrates a set of strings. The user 
08:    can add and remove strings.
09: */
10: public class SetTest
11: {
12:    public static void main(String[] args)
13:    {
14:       Set names = new HashSet();
15: 
16:       boolean done = false;
17:       while (!done)
18:       {
19:          String input = JOptionPane.showInputDialog("Add name, Cancel when done");
20:          if (input == null) 
21:             done = true;
22:          else
23:          {
24:             names.add(input);
25:             print(names);
26:          }
27:       }
28: 
29:       done = false;
30:       while (!done)
31:       {
32:          String input = JOptionPane.showInputDialog("Remove name, Cancel when done");
33:          if (input == null) 
34:             done = true;
35:          else
36:          {
37:             names.remove(input);
38:             print(names);
39:          }
40:       }
41:       System.exit(0);
42:    }
43: 
44:    /**
45:       Prints the contents of a set
46:       @param s a set
47:    */
48:    private static void print(Set s)
49:    {
50:       Iterator iter = s.iterator();
51:       System.out.print("{ ");
52:       while (iter.hasNext())
53:       {
54:          System.out.print(iter.next());
55:          System.out.print(" ");
56:       }
57:       System.out.println("}");      
58:    }
59: }
60: 
61: