I am trying to write a program to perform the following operations on two priority queues.
- union.
- intersection.
- difference.
Actual:
Test1.java:46: error: incompatible types: boolean cannot be converted to PriorityQueue<String>
return pr1.addAll(pr2);
^
Test1.java:50: error: incompatible types: boolean cannot be converted to PriorityQueue<String>
return pr1.removeAll(pr2);
^
Test1.java:54: error: incompatible types: boolean cannot be converted to PriorityQueue<String>
return pr1.retainAll(pr2);
^
3 errors
Expected:
The union of the two priority queue is [Blake, George, George, Jim, Kevin, Michael, John, Katie, Kevin, Michelle, Ryan]
The difference of the two priority queue is [Blake, Jim, John, Michael]
The intersection of the two priority queue is [George, Kevin]
My attempt
package Lists_Stacks_Queues_PriorityQueues;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;
/*
name: Evan Getis
date: 04/02
program: priority queue
*/
public class Test1 {
public static void main(String args[]){
PriorityQueue<String> pr1 = new PriorityQueue<>();
pr1.add("George");
pr1.add("Jim");
pr1.add("John");
pr1.add("Blake");
pr1.add("Kevin");
pr1.add("Michael");
PriorityQueue<String> pr2 = new PriorityQueue<>();
pr2.add("George");
pr2.add("Katie");
pr2.add("Kevin");
pr2.add("Michelle");
pr2.add("Ryan");
PriorityQueue<String> result = union(pr1,pr2);
System.out.print("The union of the two priority queues is: ");
System.out.print(result.toString());
System.out.println();
result = difference(pr1, pr2);
System.out.print("The difference of the two priority queues is: ");
System.out.print(result.toString());
System.out.println();
result = intersection(pr1, pr2);
System.out.print("The interesection of the two priority queues is: ");
System.out.print(result.toString());
System.out.println();
}
public static PriorityQueue<String> union(PriorityQueue<String> pr1, PriorityQueue<String> pr2){
return pr1.addAll(pr2);
}
public static PriorityQueue<String> difference(PriorityQueue<String> pr1, PriorityQueue<String> pr2){
return pr1.removeAll(pr2);
}
public static PriorityQueue<String> intersection(PriorityQueue<String> pr1, PriorityQueue<String> pr2){
return pr1.retainAll(pr2);
}
}
Any help with this would be greatly appreciated. Thank you!
CodePudding user response:
addAll
, removeAll
, and retainAll
do not return a new PriorityQueue
; they modify the queue the method is called on and return a boolean indicating whether or not it was modified by the call. Therefore, you should return the queue itself after calling one of those methods. To prevent the original queue from being modified by these operations, make a copy of the first queue to return.
public static PriorityQueue<String> union(PriorityQueue<String> pr1, PriorityQueue<String> pr2){
PriorityQueue<String> pq = new PriorityQueue<>(pr1);
pq.addAll(pr2);
return pq;
}
public static PriorityQueue<String> difference(PriorityQueue<String> pr1, PriorityQueue<String> pr2){
PriorityQueue<String> pq = new PriorityQueue<>(pr1);
pq.removeAll(pr2);
return pq;
}
public static PriorityQueue<String> intersection(PriorityQueue<String> pr1, PriorityQueue<String> pr2){
PriorityQueue<String> pq = new PriorityQueue<>(pr1);
pq.retainAll(pr2);
return pq;
}
CodePudding user response:
Your method need to return PriorityQueue<String>
, but you are returning a boolean
. Check javadoc for addAll()
. To citate - true if this queue changed as a result of the call
.
Since addAll()
, etc., change the collection, and you are working with the same collections every time, you need something like this:
public static PriorityQueue<String> union(PriorityQueue<String> pr1, PriorityQueue<String> pr2){
PriorityQueue<String> result = new PriorityQueue<>(pr1);
result.addAll(pr2);
return result;
}
The idea is not to change the initial queues. Do the same with difference
and intersection
.