import java.util.*; /** * Illustrate some of the more common but powerful operations that can be * performed with minimal effort using the Set interface. */ //Found at www.ajug.org. Thanks! public class SetManipulator { public static void main(String args[]) { // Create the first set. Set set1 = new HashSet(); set1.add("SEE"); set1.add("DICK"); set1.add("RUN"); set1.add("FAST"); // Create the second set. Set set2 = new HashSet(); set2.add("SEE"); set2.add("JANE"); set2.add("JUMP"); set2.add("HIGH"); // Show original sets. System.out.println("Original Set 1 : " + set1); System.out.println("Original Set 2 : " + set2); System.out.println(""); // Is 2 a subset of 1? System.out.println("2 subset of 1? : " + set1.containsAll(set2)); // Union. Set union = new HashSet(set1); union.addAll(set2); System.out.println("Union : " + union); // Intersection. Set intersection = new HashSet(set1); intersection.retainAll(set2); System.out.println("Intersection : " + intersection); // Difference. Set difference = new HashSet(set1); difference.removeAll(set2); System.out.println("Difference (1-2): " + difference); } }