import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetArrayImplementation { public static void main(String[] args) { String[] array1 = { "A", "B", "C", "D" }; String[] array2 = { "A", "E", "F", "G" }; String[] array3 = { "F", "G", "H", "I" }; /* Union */ union(array1, array2, array3); /* Intersection */ intersection(array1, array2); /* Set Difference */ setDifference(array1, array2); } private static void setDifference(String[] array1, String[] array2) { Set<String> hashSet = new HashSet<String>(); hashSet.addAll(Arrays.asList(array1)); hashSet.retainAll(Arrays.asList(array2)); System.out.println("Set Difference : " + hashSet); } private static void intersection(String[] array1, String[] array2) { Set<String> hashSet = new HashSet<String>(); hashSet.addAll(Arrays.asList(array1)); hashSet.removeAll(Arrays.asList(array2)); System.out.println("Intersection :" + hashSet); } private static void union(String[] array1, String[] array2, String[] array3) { Set<String> hashSet = new HashSet<String>(); hashSet.addAll(Arrays.asList(array1)); hashSet.addAll(Arrays.asList(array2)); hashSet.addAll(Arrays.asList(array3)); System.out.println("Union :" + hashSet); } } Output: Union :[D, E, F, G, A, B, C, H, I] Intersection :[D, B, C] Set Difference : [A]
-
Blogger Comment
-
Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
0 comments :
Post a Comment