Java Collections Java Java API
Class:
java.util.PriorityQueue
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[39, 72, 41, 83, 95]
JDK 25
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[15, 78, 52, 98, 88]
JDK 25
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[58, 30, 17, 15, 26]
JDK 25
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[98, 94, 55, 38, 52]
JDK 25
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); } }
Outputthe array list: [93, 97, 47, 54, 44] the priority queue: [44, 54, 47, 93, 97]
JDK 25
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[22, 28, 64, 94, 40] [22, 28, 64, 94, 40]
JDK 25
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); } }
Outputthe tree set: [60, 54, 49, 48, 29] the priority queue: [60, 54, 49, 48, 29]
JDK 25
|
|