Question: Use the Sets and Maps Mathematics Review to understand basic set operations. Go over the practice problem provided under this module titled Sets and Maps
Use the Sets and Maps Mathematics Review to understand basic set operations. Go over the practice problem provided under this module titled Sets and Maps Practice. You may use the Sets and Maps Starter Solution to help you solve the following operations using Java set operations.
Solve the following Set Operations using Java code as shown on the sample Sets and Maps Starter Solution.
1)A
2) B
3)
4)
5)
6)
7)
8)
9)
10)
Here is the practice:
import java.util.*;
public class SetsPractice {
public static void main(String[] args) {
// Create two linked hash sets
Set set1 = new LinkedHashSet<>(Arrays.asList(
"George", "Jim", "John", "Blake", "Kevin", "Michael"));
Set set2 = new LinkedHashSet<>(Arrays.asList(
"George", "Katie", "Kevin", "Michelle", "Ryan"));
// Display the union of the two sets
Set union = new LinkedHashSet<>(set1);
union.addAll(set2);
System.out.println("Union of the two sets: " + union);
// Display the difference of the two sets
Set difference = new LinkedHashSet<>(set1);
difference.removeAll(set2);
System.out.println("Difference of the two sets: " + difference);
// Display the intersetion of the two sets
Set intersection = new LinkedHashSet<>();
for (String e: set2) {
if (set1.contains(e))
intersection.add(e);
}
System.out.println("Intersection of the two sets: " + intersection);
}
}
and here is the solution we were given:
import java.util.*;
public class Solution {
public static void main(String[] args)
{
// Create two linked hash sets
Set universe = new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d", "e"));
Set set1 = new LinkedHashSet<>(Arrays.asList("c", "d", "e"));
Set set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "d"));
// Display the union of the two sets
Set union = new LinkedHashSet<>(set1);
union.addAll(set2);
System.out.println("2. Union of the two sets: " + union);
// Display the difference of the two sets
Set difference = new LinkedHashSet<>(set1);
difference.removeAll(set2);
System.out.println("Difference of the two sets: " + difference);
// Display the intersetion of the two sets
Set intersection = new LinkedHashSet<>();
for (String e: set2) {
if (set1.contains(e))
intersection.add(e);
}
System.out.println("3. Intersection of the two sets: " + intersection);
Set compliment = new LinkedHashSet<>(universe);
compliment.removeAll(set1);
System.out.println("1. Compliment of set A: " + compliment);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
