Close

Java Collections - PriorityQueue Constructors Examples

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, 52, 43, 78, 77]




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

[23, 51, 77, 75, 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

[99, 87, 53, 74, 75]




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

[96, 32, 46, 13, 19]

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:     [88, 74, 99, 57, 62]
the priority queue: [57, 62, 99, 74, 88]




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

[15, 29, 88, 63, 49]
[15, 29, 88, 63, 49]




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:       [89, 33, 21, 18, 10]
the priority queue: [89, 33, 21, 18, 10]




See Also