In: Computer Science
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
SOURCE CODE
import java.util.*;
public class Set_example
{
public static void main(String args[])
{
Set<Integer> a = new
HashSet<Integer>();
a.addAll(Arrays.asList(new
Integer[] {21, 23, 22, 24, 28, 29, 20}));
Set<Integer> b = new
HashSet<Integer>();
b.addAll(Arrays.asList(new
Integer[] {21, 23, 27, 25, 24, 20, 27, 25}));
// To find intersection
Set<Integer> intersection =
new HashSet<Integer>(a);
intersection.retainAll(b);
System.out.print("Intersection");
System.out.println(intersection);
// To find union
Set<Integer> union = new
HashSet<Integer>(a);
union.addAll(b);
System.out.print("Union");
System.out.println(union);
// To find the symmetric
difference
Set<Integer> difference = new
HashSet<Integer>(a);
difference.removeAll(b);
System.out.print("SET
Difference");
System.out.println(difference);
}
}
OUTPUT SCREENSHOT
please give a upvote if u feel helpful.