How to Find the Intersection and Union of Two PriorityQueues in Java? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report In Java, PriorityQueue is an implementation of the Queue Interface. It can be used to provide a way to store the elements in a queue based on their priority basis. This means high-priority elements can store first compared to lower-priority elements in the queue. In this article, we will be learning how to find the intersection and union of two PriorityQueues in Java. Methods to Implement the Intersection and Union To implement the intersection and union logic using the two methods are depicted below: retainAll( ): This is a pre-defined method, and it returns the common elements of the two priority queues.addAll(): This is also a pre-defined method, and it returns the addition of elements of the two priority queues.Program to Find the Intersection and Union of Two PriorityQueues in Java Java // Java Program to find the intersection and union of two PriorityQueues import java.util.PriorityQueue; public class GfGPriorityQueue { //Main method public static void main(String[] args) { // create two PriorityQueues PriorityQueue<Integer> queue1 = new PriorityQueue<>(); PriorityQueue<Integer> queue2 = new PriorityQueue<>(); // Populate the queues queue1.add(1); queue1.add(3); queue1.add(5); queue2.add(3); queue2.add(4); queue2.add(5); // Find intersection PriorityQueue<Integer> intersection = new PriorityQueue<>(queue1); intersection.retainAll(queue2); System.out.println("Intersection of the two priorityQueues: " + intersection); // Find union PriorityQueue<Integer> union = new PriorityQueue<>(queue1); union.addAll(queue2); System.out.println("Union of the two priorityQueues: " + union); } } OutputIntersection of the two priorityQueues: [3, 5] Union of the two priorityQueues: [1, 3, 5, 3, 4, 5] Explanation of the Program:In the above program, we have created two PriorityQueues. First one is queue1 and the second one is queue2.The intersection is obtained by creating a new PriorityQueue (intersection) and retaining only the common elements. The union is obtained by creating another new PriorityQueue (union) and adding all elements from both queues. At last, the program prints the results. Create Quiz Comment K kadambalamatclo Follow 1 Improve K kadambalamatclo Follow 1 Improve Article Tags : Java Java Programs priority-queue java-priority-queue Java Examples +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like