Set Operations Java
PROGRAM TO ILLUSTRATE SET OPERATIONS
OUTPUT
Union of the two Set[0, 1, 2, 3, 4, 5, 7, 8, 9] Intersection of the two Set[0, 1, 3, 4] Difference of the two Set[2, 8, 9]
import java.util.*; public class SetExample { public static void main(String args[]) { Set<Integer> a = new HashSet<Integer>(); a.addAll(Arrays.asList(new Integer[] {1, 3, 2, 4, 8, 9, 0})); Set<Integer> b = new HashSet<Integer>(); b.addAll(Arrays.asList(new Integer[] {1, 3, 7, 5, 4, 0, 7, 5})); // To find union Set<Integer> union = new HashSet<Integer>(a); union.addAll(b); System.out.print("Union of the two Set"); System.out.println(union); // To find intersection Set<Integer> intersection = new HashSet<Integer>(a); intersection.retainAll(b); System.out.print("Intersection of the two Set"); System.out.println(intersection); // To find the symmetric difference Set<Integer> difference = new HashSet<Integer>(a); difference.removeAll(b); System.out.print("Difference of the two Set"); System.out.println(difference); } } Union of the two Set[0, 1, 2, 3, 4, 5, 7, 8, 9] Intersection of the two Set[0, 1, 3, 4] Difference of the two Set[2, 8, 9]
Comments
Post a Comment