Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.9 kB
1
Indexable
Never
import java.util.*;

public class Main123 {
public static <T> List<T> convertSetToList(Set<T> set)
{
// create a list from Set
List<T> list = new ArrayList<>(set);

// return the list
return list;
}
static void findCart(Integer arr1[],Integer arr2[], int n, int n1)
{ System.out.print("A*b :");
for (int i = 0; i < n; i++)
for (int j = 0; j < n1; j++)
System.out.print("{"+ arr1[i]+", "
+ arr2[j]+"}, ");
}
public static void main(String args[]){

Set<Integer> A = new HashSet<Integer>();
A.addAll(Arrays.asList(1,2,3,4));

Set<Integer> B = new HashSet<Integer>();
B.addAll(Arrays.asList(3,4,5,6));

/*
Union: Set of members that belong to set A "or" set B.
*/
Set<Integer> union = new HashSet<Integer>();
union.addAll(A);
union.addAll(B);

/*
Intersection: Set of members that belong to set A "and" set B.
*/
Set<Integer> intersection = new HashSet<Integer>();
intersection.addAll(A);
intersection.retainAll(B);

/*
Difference: Set of members that belong set A "and not" set B.
*/
Set<Integer> difference = new HashSet<Integer>();
difference.addAll(A);
difference.removeAll(B);

/*
Complement: Set of members that belong to set B "and not" set A.
*/
Set<Integer> complement = new HashSet<Integer>();
complement.addAll(B);
complement.removeAll(A);
List<Integer> list = convertSetToList(A);
List<Integer> list1 = convertSetToList(B);
int n1 = list.size();
int n2 = list1.size();
Integer[] arr1 = new Integer[list.size()];
arr1 = list.toArray(arr1);
Integer[] arr2 = new Integer[list1.size()];
arr2 = list1.toArray(arr2);

//System.out.println(n1);
System.out.println("A: " + A);
System.out.println("B: " + B);
findCart(arr1, arr2, n1, n2);
System.out.println("union: " + union);
System.out.println("intersection: " + intersection);
System.out.println("difference: " + difference);
System.out.println("complement: " + complement);

}
}