Question: This is to be done in Java using Sets. The task is: Fill an ArrayList with a random number (between 2 and 12) of HashSets
This is to be done in Java using Sets.
The task is:
Fill an ArrayList with a random number (between 2 and 12) of HashSets that each hold exactly 10 random numbers between 1 and 30. All of the sets must have sizes equal to 10. Print each set.
Create a method that will receive two sets and return a set of the intersection of the sets. The intersection will consist only of the common values contained within all sets.
Create a method that will receive two sets and return a set of the union of the sets. The union will consist only of the unique values contained within all sets.
Use these two methods to create and print the intersection and union sets for all of the sets in the ArrayList.
Sometimes, some sets in the intersection will be empty. Hence, try to run until a non-empty intersection is obtained.
SAMPLE OUTPUT
Set 1 => [1, 20, 21, 23, 25, 10, 26, 27, 11, 28]
Set 2 => [16, 1, 17, 5, 22, 6, 7, 8, 27, 28]
Set 3 => [17, 1, 19, 3, 20, 22, 24, 26, 14, 15]
Intersection => [1]
union = > [1, 3, 5, 6, 7, 8, 10, 11, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
