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