Close

Java Collections - PriorityQueue Constructors Examples

[Last Updated: Oct 30, 2025]

Java Collections Java Java API 


Class:

java.util.PriorityQueue

java.lang.Objectjava.lang.Objectjava.util.AbstractCollectionjava.util.AbstractCollectionjava.util.CollectionCollectionjava.util.AbstractQueuejava.util.AbstractQueuejava.util.QueueQueuejava.util.PriorityQueuejava.util.PriorityQueuejava.io.SerializableSerializableLogicBig

Constructors:

public PriorityQueue()

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



public PriorityQueue(int initialCapacity)

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



public PriorityQueue(Comparator<? super E> comparator)

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



public PriorityQueue(int initialCapacity,
                     Comparator<? super E> comparator)

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



public PriorityQueue(Collection<? extends E> c)

Creates a PriorityQueue containing the elements in the specified collection. If the specified collection is an instance of a SortedSet or is another PriorityQueue , this priority queue will be ordered according to the same ordering. Otherwise, this priority queue will be ordered according to the natural ordering of its elements.



public PriorityQueue(PriorityQueue<? extends E> c)

Creates a PriorityQueue containing the elements in the specified priority queue. This priority queue will be ordered according to the same ordering as the given priority queue.



public PriorityQueue(SortedSet<? extends E> c)

Creates a PriorityQueue containing the elements in the specified sorted set. This priority queue will be ordered according to the same ordering as the given sorted set.


Examples


PriorityQueue<>() Example:

package com.logicbig.example.priorityqueue;

import java.util.PriorityQueue;
import java.util.concurrent.ThreadLocalRandom;

public class PriorityQueueExample {

public static void main(String... args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 0; i < 5; i++) {
pq.add(ThreadLocalRandom.current().nextInt(10, 100));
}

System.out.println(pq);
}
}

Output

[37, 60, 71, 67, 65]




PriorityQueue(initialCapacity) Example:

package com.logicbig.example.priorityqueue;

import java.util.PriorityQueue;
import java.util.concurrent.ThreadLocalRandom;

public class PriorityQueueExample2 {

public static void main(String... args) {
PriorityQueue<Integer> pq = new PriorityQueue<>(5);
for (int i = 0; i < 5; i++) {
pq.add(ThreadLocalRandom.current().nextInt(10, 100));
}
System.out.println(pq);
}
}

Output

[22, 41, 68, 81, 61]




PriorityQueue(comparator) Example:

package com.logicbig.example.priorityqueue;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.concurrent.ThreadLocalRandom;

public class PriorityQueueExample3 {

public static void main(String... args) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
for (int i = 0; i < 5; i++) {
pq.add(ThreadLocalRandom.current().nextInt(10, 100));
}

System.out.println(pq);
}
}

Output

[56, 46, 42, 12, 17]




PriorityQueue(initialCapacity, comparator) Example:

package com.logicbig.example.priorityqueue;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.concurrent.ThreadLocalRandom;

public class PriorityQueueExample4 {

public static void main(String... args) {
PriorityQueue<Integer> pq = new PriorityQueue<>(5,
Comparator.reverseOrder());
for (int i = 0; i < 5; i++) {
pq.add(ThreadLocalRandom.current().nextInt(10, 100));
}
System.out.println(pq);

}
}

Output

[94, 89, 75, 30, 87]

PriorityQueue(collection) Example:

package com.logicbig.example.priorityqueue;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.concurrent.ThreadLocalRandom;

public class PriorityQueueExample5 {

public static void main(String... args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(ThreadLocalRandom.current().nextInt(10, 100));
}
System.out.println("the array list: " + list);
PriorityQueue<Integer> pq = new PriorityQueue<>(list);
System.out.println("the priority queue: " + pq);
}
}

Output

the array list:     [48, 24, 80, 68, 44]
the priority queue: [24, 44, 80, 68, 48]




PriorityQueue(priorityQueue) Example:

package com.logicbig.example.priorityqueue;

import java.util.PriorityQueue;
import java.util.concurrent.ThreadLocalRandom;

public class PriorityQueueExample6 {

public static void main(String... args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 0; i < 5; i++) {
pq.add(ThreadLocalRandom.current().nextInt(10, 100));
}
System.out.println(pq);
PriorityQueue<Integer> pq2 = new PriorityQueue<>(pq);
System.out.println(pq2);
}
}

Output

[46, 55, 62, 64, 59]
[46, 55, 62, 64, 59]




PriorityQueue<>(sortedSet) Example:

package com.logicbig.example.priorityqueue;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class PriorityQueueExample7 {

public static void main(String... args) {
SortedSet<Integer> set = new TreeSet<>(Comparator.reverseOrder());
for (int i = 0; i < 5; i++) {
set.add(ThreadLocalRandom.current().nextInt(10, 100));
}
System.out.println("the tree set: " + set);
PriorityQueue<Integer> pq = new PriorityQueue<>(set);
System.out.println("the priority queue: " + pq);
}
}

Output

the tree set:       [92, 76, 55, 37, 28]
the priority queue: [92, 76, 55, 37, 28]




See Also