import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SymmetricDifference { public static void main(String[] args) { Set setA = new HashSet(Arrays.asList("John", "Serena", "Bob", "Mary", "Serena")); Set setB = new HashSet(Arrays.asList("Jim", "Mary", "John", "Jim", "Bob")); // Present our initial data set System.out.println("In set A: " + setA); System.out.println("In set B: " + setB); // Option 1: union of differences // Get our individual differences. Set notInSetA = new HashSet(setB); notInSetA.removeAll(setA); Set notInSetB = new HashSet(setA); notInSetB.removeAll(setB); // The symmetric difference is the concatenation of the two individual differences Set symmetricDifference = new HashSet(notInSetA); symmetricDifference.addAll(notInSetB); // Option 2: union minus intersection // Combine both sets Set union = new HashSet(setA); union.addAll(setB); // Get the intersection Set intersection = new HashSet(setA); intersection.retainAll(setB); // The symmetric difference is the union of the 2 sets minus the intersection Set symmetricDifference2 = new HashSet(union); symmetricDifference2.removeAll(intersection); // Present our results System.out.println("Not in set A: " + notInSetA); System.out.println("Not in set B: " + notInSetB); System.out.println("Symmetric Difference: " + symmetricDifference); System.out.println("Symmetric Difference 2: " + symmetricDifference2); } }