Skip to content

Commit 8b33a15

Browse files
author
jsquared21
committed
Add Ex 20.10
1 parent b6b6f68 commit 8b33a15

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**************************************************************************
2+
* (Perform set operations on priority queues) Create two priority queues, *
3+
* {"George", "Jim", "John", "Blake", "Kevin", "Michael"} and *
4+
* {"George", "Katie", "Kevin", "Michelle", "Ryan"}, and find their union, *
5+
* difference, and intersection. *
6+
**************************************************************************/
7+
import java.util.*;
8+
9+
public class Exercise_20_10 {
10+
public static void main(String[] args) {
11+
// Create two priority queues
12+
PriorityQueue<String> queue1 = new PriorityQueue<>(Arrays.asList(
13+
"George", "Jim", "John", "Blake", "Kevin", "Michael"));
14+
15+
PriorityQueue<String> queue2 = new PriorityQueue<>(Arrays.asList(
16+
"George", "Katie", "Kevin", "Michelle", "Ryan"));
17+
18+
// Display the two sets and union, difference, and intersection
19+
System.out.println("Set1: " + queue1);
20+
System.out.println("Set2: " + queue2);
21+
System.out.println("Union of sets: " + union(queue1, queue2));
22+
System.out.println("Difference of sets: " + difference(queue1, queue2));
23+
System.out.println("Intersection of sets: " + intersection(queue1, queue2));
24+
}
25+
26+
/** Method returns the union of two sets */
27+
private static <T> PriorityQueue<T> union(
28+
PriorityQueue<T> set1, PriorityQueue<T> set2) {
29+
PriorityQueue<T> union = new PriorityQueue<>(set1);
30+
union.addAll(set2);
31+
return union;
32+
}
33+
34+
/** Method returns the difference of two sets */
35+
private static <T> PriorityQueue<T> difference(
36+
PriorityQueue<T> set1, PriorityQueue<T> set2) {
37+
PriorityQueue<T> difference = new PriorityQueue<>(set1);
38+
difference.removeAll(set2);
39+
return difference;
40+
}
41+
42+
/** Method returns the intersection of two sets */
43+
private static <T> PriorityQueue<T> intersection(
44+
PriorityQueue<T> set1, PriorityQueue<T> set2) {
45+
PriorityQueue<T> intersection = new PriorityQueue<>(set1);
46+
intersection.retainAll(set2);
47+
return intersection;
48+
}
49+
}

0 commit comments

Comments
 (0)