GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • heap data structure
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — PriorityQueue in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

PriorityQueue in Java

Last Updated : 31 Aug, 2026

A PriorityQueue in Java is a queue where elements are ordered based on their priority, rather than the order of insertion. By default, it uses natural ordering (min-heap), but a custom comparator can be used to define different priorities.

  • Supports standard queue operations like add(), poll(), and peek().
  • Automatically grows as elements are added and null insertion is not possible .
  • Uses a heap data structure internally to ensure efficient insertion and removal of the highest-priority element.
Java
import java.util.PriorityQueue;

public class Geeks 
{
    public static void main(String[] args) 
    {
      	// Priority Queue Min Type
        PriorityQueue<Integer> p = new PriorityQueue<>();

        // Add elements to the queue
        p.add(3);
        p.add(10);
        p.add(7);
        p.add(2);

        // Print the head of the queue
        System.out.println("Head of Queue: " + p.peek());
    }
}

Output
Head of Queue: 2

Declaration of PriorityQueue

public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable, Iterable<E>

where, E is the type of elements held in this queue.

Hierarchy of PriorityQueue

PriorityQueue extends AbstractQueue and implements the Queue interface, which is part of the Collection hierarchy. It also supports iteration through Iterable

iterable
PriorityQueue Hierarchy

Constructors of PriorityQueue

1. PriorityQueue()

This method creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.

PriorityQueue<E> pq = new PriorityQueue<E>();

2. PriorityQueue(int initialCapacity)

This creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.

PriorityQueue<E> pq = new PriorityQueue<E>(initialCapacity);

3. PriorityQueue(Comparator<E> comparator)

This creates a PriorityQueue with the default initial capacity and whose elements are ordered according to the specified comparator.

Comparator<Integer> c = (a, b) -> b - a; // max-heap
PriorityQueue<Integer> pq = new PriorityQueue<>(c);

4. PriorityQueue(int initialCapacity, Comparator<E> comparator)

This creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

PriorityQueue<E> pq = new PriorityQueue<E>(int initialCapacity, Comparator<E> comparator);

Different Operations on PriorityQueue

Let’s see how to perform a few frequently used operations on the Priority Queue class.

1. Adding Elements

To add an element in a priority queue, we can use the add() method.

Java
import java.util.*;
  
public class Geeks
{  
    public static void main(String args[])
    {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        
      	for(int i=0;i<3;i++){
            pq.add(i);
            pq.add(1);
        }
      
        System.out.println(pq);
    }
}

Output
[0, 1, 1, 1, 2, 1]

2. Removing Elements

To remove an element from a priority queue, we can use the remove() method.

Java
import java.util.*;
import java.io.*;

public class Geeks {

    public static void main(String args[])
    {
        PriorityQueue<String> pq = new PriorityQueue<>();

        pq.add("Geeks");
        pq.add("For");
        pq.add("Geeks");

        System.out.println("Initial PriorityQueue " + pq);

        // using the method
        pq.remove("Geeks");

        System.out.println("After Remove: " + pq);

        System.out.println("Poll Method: " + pq.poll());

        System.out.println("Final PriorityQueue: " + pq);
    }
}

Output
Initial PriorityQueue [For, Geeks, Geeks]
After Remove: [For, Geeks]
Poll Method: For
Final PriorityQueue: [Geeks]

3. Accessing the Elements

To access elements from a priority queue, we can use the peek() method.

Java
import java.util.*;

class Geeks {
    public static void main(String[] args)
    {

        PriorityQueue<String> pq = new PriorityQueue<>();
        pq.add("Geeks");
        pq.add("For");
        pq.add("Geeks");
        System.out.println("PriorityQueue: " + pq);

        // Using the peek() method
        String element = pq.peek();
        System.out.println("Accessed Element: " + element);
    }
}

Output
PriorityQueue: [For, Geeks, Geeks]
Accessed Element: For

4. Iterating the PriorityQueue

There are multiple ways to iterate through the PriorityQueue. The most famous way is converting the queue to the array and traversing using an iterator. Iterator does not traverse in priority order.

Java
import java.util.*;

public class Geeks {
    public static void main(String args[])
    {
        PriorityQueue<String> pq = new PriorityQueue<>();

        pq.add("Geeks");
        pq.add("For");
        pq.add("Geeks");

        Iterator<String> iterator = pq.iterator();

        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
}

Output
For Geeks Geeks 

Methods of PriorityQueue

MethodDescription
add(E e)Inserts the specified element into this priority queue.
clear()Removes all of the elements from this priority queue.
comparator()Returns the comparator used to order the elements in this queue or null if this queue is sorted according to the natural ordering of its elements.
contains(Object o)Returns true if this queue contains the specified element.
forEach(Consumer<? super E> action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
iterator()Returns an iterator over the elements in this queue.
 offer(E e)Inserts the specified element into this priority queue.
remove(Object o)Removes a single instance of the specified element from this queue, if it is present.
removeAll(Collection<?> c)Removes all of this collection's elements that are also contained in the specified collection (optional operation).
removeIf(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.
retainAll(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).
spliterator()Creates a late-binding and fail-fast Spliterator over the elements in this queue.
 toArray()Returns an array containing all of the elements in this queue.
 toArray(T[] a)Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.

Methods Declared in Interface java.util.Queue

MethodDescription
peek()Retrieves, but does not remove, the head of this queue or returns null if this queue is empty.
poll()Retrieves and removes the head of this queue or returns null if this queue is empty.

Methods Declared in Class java.util.AbstractQueue

MethodDescription
addAll(Collection<? extends E> c)Adds all of the elements in the specified collection to this queue.
element()Retrieves, but does not remove, the head of this queue.
remove()Retrieves and removes the head of this queue.

Methods Declared in Class java.util.AbstractCollection

MethodDescription
containsAll(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.
isEmpty()Returns true if this collection contains no elements.
toString()Returns a string representation of this collection.

Methods Declared in Interface java.util.Collection

MethodDescription
containsAll(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.
equals(Object o)Compares the specified object with this collection for equality.
hashCode()Returns the hash code value for this collection.
isEmpty()Returns true if this collection contains no elements.
size()Returns the number of elements in this collection.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.