Monday, 16 January 2017

Chapter 20 Exercise 10, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

20.10 (Perform set operations on priority queues)
Create two priority queues,
{"George", "Jim", "John", "Blake", "Kevin", "Michael"}
{"George", "Katie", "Kevin", "Michelle", "Ryan"},
and find their union, difference, and intersection.

import java.util.Arrays;
import java.util.PriorityQueue;

public class Exercise_10 {

public static void main(String[] args) {

PriorityQueue<String> pq1 = new PriorityQueue<>(Arrays.asList("George", "Jim", "John", "Blake", "Kevin", "Michael"));
PriorityQueue<String> pq2 = new PriorityQueue<>(Arrays.asList("George", "Katie", "Kevin", "Michelle", "Ryan"));

PriorityQueue<String> union = new PriorityQueue<>(pq1);
PriorityQueue<String> tmp = new PriorityQueue<>(pq2);

String s;
while ((s = tmp.poll()) != null) {
if (!union.contains(s)) {
}
}
System.out.println("Union set: " + union);

PriorityQueue<String> difference = new PriorityQueue<>(pq1);
difference.removeAll(pq2);
System.out.println("Difference: " + difference);

PriorityQueue<String> intersection = new PriorityQueue<>(pq1);
intersection.retainAll(pq2);
System.out.println("Difference: " + intersection);

}

}