Source of Sets.java


  1: import java.util.Scanner;
  2: import java.util.Set;
  3: import java.util.HashSet;
  4: import java.util.TreeSet;

  6: /**
  7:  * A program to demonstrate some basics of Sets.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class Sets {

 13:     private static Scanner kbd = new Scanner(System.in);

 15:     public static void main(String[] args) {
 16:         // create some set variables and a set object
 17:         Set<String> names1 = new HashSet<String>();
 18:         Set<String> set1, set2;

 20:         // set basics -- create and add
 21:         System.out.println("\nSets start empty.");
 22:         System.out.println("\tnames1: " + names1);
 23:         System.out.println("\tnames1.isEmpty(): " + names1.isEmpty());
 24:         System.out.println("After we add elements, they aren't empty.");
 25:         names1.add("Alex");
 26:         names1.add("Betty");
 27:         names1.add("Carol");
 28:         names1.add("David");
 29:         System.out.println("\tnames1: " + names1);
 30:         System.out.println("\tnames1.isEmpty(): " + names1.isEmpty());
 31:         pause();

 33:         // copy a set
 34:         System.out.println("\nNow some manipulations:");
 35:         set1 = new HashSet<String>(names1);  // copy set1
 36:         set2 = new HashSet<String>();
 37:         set2.add("Andrew");
 38:         set2.add("Betty");
 39:         set2.add("Chris");
 40:         System.out.println("set1.addAll(set2)");
 41:         System.out.println("\tset1 starts as: " + set1);
 42:         System.out.println("\tset2 starts as: " + set2);
 43:         set1.addAll(set2);
 44:         System.out.println("\tset1 ends as: " + set1);
 45:         System.out.println("\tset2 ends as: " + set2);
 46:         pause();

 48:         // removeAll removes multiple elements from a set.
 49:         set1 = new HashSet<String>(names1);  // restore set1 to old value
 50:         System.out.println("set1.removeAll(set2)");
 51:         System.out.println("\tset1 starts as: " + set1);
 52:         System.out.println("\tset2 starts as: " + set2);
 53:         set1.removeAll(set2);
 54:         System.out.println("\tset1 ends as: " + set1);
 55:         System.out.println("\tset2 ends as: " + set2);
 56:         pause();

 58:         // clear empties a set
 59:         set1 = new HashSet<String>(names1);  // restore set1 to old value
 60:         System.out.println("set1.clear()");
 61:         System.out.println("\tset1 starts as: " + set1);
 62:         set1.clear();
 63:         System.out.println("\tset1 ends as: " + set1);
 64:         pause();

 66:         // containsAll checks for multiple elements of a set.
 67:         set1 = new HashSet<String>(names1);  // restore set1 to old value
 68:         System.out.println("set1.containsAll(set2)");
 69:         System.out.println("\tset1 is: " + set1);
 70:         System.out.println("\tset2 is: " + set2);
 71:         System.out.println("\tset1.containsAll(set2) is: "
 72:                 + set1.containsAll(set2));
 73:         pause();

 75:         // different kinds of sets list objects in different orders
 76:         Set<String> names2 = new TreeSet<String>(names1);
 77:         System.out.println("names2 is a copy of names1, "
 78:                 + "but using a TreeSet instead of a HashSet.\n"
 79:                 + "\tnames2 is: " + names2);
 80:         pause();

 82:         // containsAll again
 83:         System.out.println("names1.containsAll(names2)");
 84:         System.out.println("\tnames1 is: " + names1);
 85:         System.out.println("\tnames2 is: " + names2);
 86:         System.out.println("\tnames1.containsAll(names2) is: "
 87:                 + names1.containsAll(names2));
 88:         pause();
 89:     }

 91:     /**
 92:      * Prompt the user and wait for them to press the enter key.
 93:      */
 94:     public static void pause() {
 95:         System.out.print("Press Enter...");
 96:         kbd.nextLine();
 97:         System.out.println();
 98:     }

100: }