Java中的 PriorityBlockingQueue 类
PriorityBlockingQueue是一个无界阻塞队列,它使用与类PriorityQueue相同的排序规则并提供阻塞检索操作。由于它是无界的,添加元素有时可能会由于资源耗尽而失败,从而导致OutOfMemoryError 。此类不允许空元素。
PriorityBlockingQueue 类及其迭代器实现了 Collection 和 Iterator 接口的所有可选方法。
方法 iterator() 中提供的 Iterator 和方法 spliterator() 中提供的 Spliterator 不能保证以任何特定顺序遍历 PriorityBlockingQueue 的元素。对于有序遍历,使用Arrays.sort(pq.toArray()) 。此外,方法 drainTo() 可用于按优先级顺序删除部分或所有元素并将它们放置在另一个集合中。
此类上的操作不保证具有相同优先级的元素的顺序。如果需要强制执行排序,请定义自定义类或比较器,它们使用辅助键来打破主要优先级值的关系。
此类是Java集合框架的成员。
PriorityBlockingQueue 的层次结构
它实现了 Serializable 、 Iterable
宣言:
public class PriorityBlockingQueue
这里, E是该集合中包含的元素的类型。
PriorityBlockingQueue 的构造函数
为了创建 PriorityBlockingQueue 的实例,我们需要从Java.util.concurrent.PriorityBlockingQueue导入它。
1. PriorityBlockingQueue() – 创建一个具有默认初始容量 (11) 的 PriorityBlockingQueue,它根据元素的自然顺序对其元素进行排序。添加超过初始容量的元素会动态更改 PriorityBlockingQueue 的容量,因为 PriorityBlockingQueue 不受容量限制。
PriorityBlockingQueue
例子:
Java
// Java program to demonstrate
// PriorityBlockingQueue() constructor
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// create object of PriorityBlockingQueue
// using PriorityBlockingQueue() constructor
PriorityBlockingQueue pbq
= new PriorityBlockingQueue();
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
pbq.add(4);
pbq.add(5);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
Java
// Java program to demonstrate
// PriorityBlockingQueue(Collection c) constructor
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating a Collection
Vector v = new Vector();
v.addElement(1);
v.addElement(2);
v.addElement(3);
v.addElement(4);
v.addElement(5);
// create object of PriorityBlockingQueue
// using PriorityBlockingQueue(Collection c)
// constructor
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(v);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
Java
// Java program to demonstrate
// PriorityBlockingQueue(int initialCapacity)
// constructor
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
// using PriorityBlockingQueue(int initialCapacity)
// constructor
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(capacity);
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
Java
// Java program to demonstrate
// PriorityBlockingQueue(int initialCapacity, Comparator
// comparator) constructor
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(
capacity, Comparator.reverseOrder());
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
Java
// Java program to demonstrate adding elements
// to the PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class AddingElementsExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(capacity);
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
Java
// Java program to demonstrate removing
// elements from the PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class RemovingElementsExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(capacity);
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
// remove all the elements
pbq.clear();
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
Java
// Java Program Demonstrate accessing
// elements of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class AccessingElementsExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue PrioQueue
= new PriorityBlockingQueue(
capacityOfQueue);
// Add elements to PriorityBlockingQueue
PrioQueue.add(464161);
PrioQueue.add(416165);
// print PrioQueue
System.out.println("PrioQueue: " + PrioQueue);
// get head of PriorityBlockingQueue
int head = PrioQueue.peek();
// print head of PriorityBlockingQueue
System.out.println("Head of Queue: " + head);
}
}
Java
// Java Program Demonstrate iterating
// over PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class IteratingExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue names
= new PriorityBlockingQueue(
capacityOfQueue);
// Add names of students of girls college
names.add("Geeks");
names.add("forGeeks");
names.add("A");
names.add("Computer");
names.add("Portal");
// Call iterator() method of PriorityBlockingQueue
Iterator iteratorVals = names.iterator();
// Print elements of iterator
// created from PriorityBlockingQueue
System.out.println("The Names are:");
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
Java
// Java Program Demonstrate comparator()
// method and passing Comparator to PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class ComparatorExample {
public static void main(String[] args)
throws InterruptedException
{
// create object of PriorityBlockingQueue
PriorityBlockingQueue PrioQueue
= new PriorityBlockingQueue(
10, new Comparator() {
public int compare(Integer a, Integer b)
{
return a - b;
}
});
// Add numbers to PriorityBlockingQueue
PrioQueue.put(45815616);
PrioQueue.put(4981561);
PrioQueue.put(4594591);
PrioQueue.put(9459156);
// get String representation of
// PriorityBlockingQueue
String str = PrioQueue.toString();
// Creating a comparator using comparator()
Comparator comp = PrioQueue.comparator();
// Displaying the comparator values
System.out.println("Comparator value: " + comp);
if (comp == null)
System.out.println(
"PriorityBlockingQueue follows natural ordering");
else
System.out.println(
"PriorityBlockingQueue follows : " + comp);
}
}
PriorityBlockingQueue:[1, 2, 3, 4, 5]
2. PriorityBlockingQueue(Collection extends E> c) – 创建一个包含指定集合中元素的 PriorityBlockingQueue。
PriorityBlockingQueue
例子:
Java
// Java program to demonstrate
// PriorityBlockingQueue(Collection c) constructor
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating a Collection
Vector v = new Vector();
v.addElement(1);
v.addElement(2);
v.addElement(3);
v.addElement(4);
v.addElement(5);
// create object of PriorityBlockingQueue
// using PriorityBlockingQueue(Collection c)
// constructor
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(v);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
PriorityBlockingQueue:[1, 2, 3, 4, 5]
3. PriorityBlockingQueue(int initialCapacity) – 创建一个具有指定初始容量的 PriorityBlockingQueue,它根据元素的自然顺序对其元素进行排序。例子:
Java
// Java program to demonstrate
// PriorityBlockingQueue(int initialCapacity)
// constructor
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
// using PriorityBlockingQueue(int initialCapacity)
// constructor
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(capacity);
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
PriorityBlockingQueue:[1, 2, 3]
4. PriorityBlockingQueue(int initialCapacity, Comparator super E>comparator) – 创建一个具有指定初始容量的 PriorityBlockingQueue,它根据指定的比较器对其元素进行排序。例子:
Java
// Java program to demonstrate
// PriorityBlockingQueue(int initialCapacity, Comparator
// comparator) constructor
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(
capacity, Comparator.reverseOrder());
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
PriorityBlockingQueue:[3, 1, 2]
基本操作
1.添加元素
PriorityBlockingQueue 的 add(E e) 方法将作为参数传递的元素插入到此 PriorityBlockingQueue 尾部的方法中。如果添加元素成功,则此方法返回 true。否则返回false。
Java
// Java program to demonstrate adding elements
// to the PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class AddingElementsExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(capacity);
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
PriorityBlockingQueue:[1, 2, 3]
2. 移除元素
PriorityBlockingQueue 的 remove(Object o) 方法用于从该队列中删除一个元素。此方法删除作为参数传递的元素的单个实例(如果存在)。当且仅当元素被移除时,它返回 true,否则返回 false。 clear() 用于一次删除所有元素。
Java
// Java program to demonstrate removing
// elements from the PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class RemovingElementsExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue pbq
= new PriorityBlockingQueue(capacity);
// add numbers
pbq.add(1);
pbq.add(2);
pbq.add(3);
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
// remove all the elements
pbq.clear();
// print queue
System.out.println("PriorityBlockingQueue:" + pbq);
}
}
PriorityBlockingQueue:[1, 2, 3]
PriorityBlockingQueue:[]
3. 访问元素
PriorityBlockingQueue 的 peek() 方法返回 PriorityBlockingQueue 头部的元素。它检索 LinkedBlockingQueue 头部的值,但不删除它。如果 PriorityBlockingQueue 不包含任何元素,则此方法返回 null。 PriorityBlockingQueue 队列使用与类 PriorityQueue 相同的排序规则。
Java
// Java Program Demonstrate accessing
// elements of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class AccessingElementsExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue PrioQueue
= new PriorityBlockingQueue(
capacityOfQueue);
// Add elements to PriorityBlockingQueue
PrioQueue.add(464161);
PrioQueue.add(416165);
// print PrioQueue
System.out.println("PrioQueue: " + PrioQueue);
// get head of PriorityBlockingQueue
int head = PrioQueue.peek();
// print head of PriorityBlockingQueue
System.out.println("Head of Queue: " + head);
}
}
PrioQueue: [416165, 464161]
Head of Queue: 416165
4. 迭代
PriorityBlockingQueue 类的 iterator() 方法返回此队列中元素的迭代器。此方法返回的元素不遵循任何顺序。返回的迭代器是弱一致的。
Java
// Java Program Demonstrate iterating
// over PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class IteratingExample {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue names
= new PriorityBlockingQueue(
capacityOfQueue);
// Add names of students of girls college
names.add("Geeks");
names.add("forGeeks");
names.add("A");
names.add("Computer");
names.add("Portal");
// Call iterator() method of PriorityBlockingQueue
Iterator iteratorVals = names.iterator();
// Print elements of iterator
// created from PriorityBlockingQueue
System.out.println("The Names are:");
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
The Names are:
A
Computer
Geeks
forGeeks
Portal
5. 比较器示例
PriorityBlockingQueue 的comparator() 方法返回可用于对PriorityBlockingQueue 中的元素进行排序的比较器。如果队列遵循元素的自然排序模式,则该方法返回空值。
Java
// Java Program Demonstrate comparator()
// method and passing Comparator to PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class ComparatorExample {
public static void main(String[] args)
throws InterruptedException
{
// create object of PriorityBlockingQueue
PriorityBlockingQueue PrioQueue
= new PriorityBlockingQueue(
10, new Comparator() {
public int compare(Integer a, Integer b)
{
return a - b;
}
});
// Add numbers to PriorityBlockingQueue
PrioQueue.put(45815616);
PrioQueue.put(4981561);
PrioQueue.put(4594591);
PrioQueue.put(9459156);
// get String representation of
// PriorityBlockingQueue
String str = PrioQueue.toString();
// Creating a comparator using comparator()
Comparator comp = PrioQueue.comparator();
// Displaying the comparator values
System.out.println("Comparator value: " + comp);
if (comp == null)
System.out.println(
"PriorityBlockingQueue follows natural ordering");
else
System.out.println(
"PriorityBlockingQueue follows : " + comp);
}
}
Comparator value: ComparatorExample$1@27bc2616
PriorityBlockingQueue follows : ComparatorExample$1@27bc2616
PriorityBlockingQueue 的方法
METHOD | DESCRIPTION |
---|---|
add(E e) | Inserts the specified element into this priority queue. |
clear() | Atomically removes all of the elements from this queue. |
comparator() | Returns the comparator used to order the elements in this queue, or null if this queue uses the natural ordering of its elements. |
contains(Object o) | Returns true if this queue contains the specified element. |
drainTo(Collection super E> c) | Removes all available elements from this queue and adds them to the given collection. |
drainTo(Collection super E> c, int maxElements) | Removes at most the given number of available elements from this queue and adds them to the given collection. |
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. |
offer(E e, long timeout, TimeUnit unit) | Inserts the specified element into this priority queue. |
put(E e) | Inserts the specified element into this priority queue. |
remainingCapacity() | Always returns Integer.MAX_VALUE because a PriorityBlockingQueue is not capacity constrained. |
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() | Returns a 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. |
在类Java.util.AbstractQueue 中声明的方法
METHOD | DESCRIPTION |
---|---|
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. |
在类Java.util.AbstractCollection 中声明的方法
METHOD | DESCRIPTION |
---|---|
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. |
在接口Java.util.concurrent.BlockingQueue 中声明的方法
METHOD | DESCRIPTION |
---|---|
poll(long timeout, TimeUnit unit) | Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available. |
take() | Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. |
在接口Java.util.Collection 中声明的方法
METHOD | DESCRIPTION |
---|---|
addAll(Collection extends E> c) | Adds all of the elements in the specified collection to this collection (optional operation). |
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. |
parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
size() | Returns the number of elements in this collection. |
stream() | Returns a sequential Stream with this collection as its source. |
toArray(IntFunction | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
在接口Java .util.Queue 中声明的方法
METHOD | DESCRIPTION |
---|---|
element() | Retrieves, but does not remove, the head of this queue. |
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. |
remove() | Retrieves and removes the head of this queue. |
参考: Java : Java