Reputation: 33
My program prints the main constructor but is not returning the 3 bottom functions but its compiling. Where am I going wrong?
import java.util.*;
public class Main {
public static void main(String[] args) {
PriorityQueue<String> queue1 = new PriorityQueue<String>();
System.out.println(" ");
//Adding strings to queue1
queue1.offer("Goerge");
queue1.offer("Jim");
queue1.offer("John");
queue1.offer("Blake");
queue1.offer("Kevin");
queue1.offer("Michael");
System.out.println("Priority queue using Comparable:");
while (queue1.size() > 0) {
System.out.print(queue1.remove() + " ");
}
PriorityQueue<String> queue2 = new PriorityQueue<String>(4,
Collections.reverseOrder());
System.out.println(" ");
//Adding strings to queue2
queue2.offer("George");
queue2.offer("Katie");
queue2.offer("Kevin");
queue2.offer("Michelle");
queue2.offer("Ryan");
System.out.println("\nPriority queue using Comparator:");
while (queue2.size() > 0) {
System.out.print(queue2.remove() + " ");
}
}
public static PriorityQueue<String> union(PriorityQueue<String> queue1, PriorityQueue<String> queue2){
PriorityQueue<String> queue3 = new PriorityQueue<>();
queue3.addAll(queue1);
queue3.addAll(queue2);
return queue3;
}
public static PriorityQueue<String> intersection(PriorityQueue<String> queue1, PriorityQueue<String> queue2) {
PriorityQueue<String> queue3 = new PriorityQueue<>(queue1);
queue3.retainAll(new HashSet<>(queue2));
return queue3;
}
public static PriorityQueue<String> difference(PriorityQueue<String> q1, PriorityQueue<String> intersectionQueue) {
PriorityQueue<String> q3 = new PriorityQueue<>(q1);
q3.removeAll(new HashSet<>(intersectionQueue));
return q3;
}
}
The 3 functions must return the union, difference and intersection of the 2 queues. But they are not displaying in output.
Upvotes: 0
Views: 1077
Reputation: 71
package priorityQueues;
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
/**
* @author felendernukeri
*
*/
public class UnionDifferenceIntersection {
/**
* @param args
*/
public static void main(String[] args) {
// Create two priority queues
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"));
// Display the two sets and union, difference, and intersection
System.out.println("Set 1: " + pq1);
System.out.println("Set 2: " + pq2);
System.out.println("Union: " + findUnion(pq1, pq2));
System.out.println("Difference: " + findDifference(pq1, pq2));
System.out.println("Intersection: " + findIntersection(pq1, pq2));
// Initiate hashSet
HashSet<String> hashSet = new HashSet<>();
}
/** Calculating Union. */
public static <E> PriorityQueue<E> findUnion(PriorityQueue<E> p1, PriorityQueue<E> p2) {
PriorityQueue<E> union = new PriorityQueue<>(p1);
union.addAll(p2);
return union;
}
/** Calculating difference. */
public static <E> PriorityQueue <E> findDifference(PriorityQueue<E> p1, PriorityQueue<E> p2){
PriorityQueue<E> difference = new PriorityQueue<>(p1);
difference.removeAll(p2);
return difference;
}
/** Calculating Intersection. */
public static <E> PriorityQueue <E> findIntersection(PriorityQueue<E> p1, PriorityQueue<E> p2){
PriorityQueue<E> intersection = new PriorityQueue<>(p1);
intersection.retainAll(p2);
return intersection;
}
}
Upvotes: 0
Reputation: 503
I am guessing two things,
One: as the comments point out, you are not calling the functions, In which case, just call them from your main method.
Two: You are calling union on both queue1 and queue2 after printing the output in the while loop. In your print statement you are removing values from the queue and so when you call the union function, both the queues are empty , thus no output. If you need to print data use
System.out.println(Arrays.toString(priorityQueue.toArray()));
instead of removing elements from a priority queue
Upvotes: 1