版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/idealcitier/article/details/88410747
当应该基于优先级处理对象时,使用PriorityQueue。 众所周知,队列遵循先进先出算法,但有时需要根据优先级处理队列的元素,即当PriorityQueue发挥作用时。 PriorityQueue基于优先级堆。 优先级队列的元素按照自然顺序排序,或者由队列构造时提供的比较器排序,具体取决于使用的构造函数。
通过下面的demo来简单的介绍一下的java的 PriorityQueue
// Java progrm to demonstrate working of priority queue in Java
import java.util.*;
class Example
{
public static void main(String args[])
{
// Creating empty priority queue
PriorityQueue<String> pQueue =
new PriorityQueue<String>();
// Adding items to the pQueue using add()
pQueue.add("C");
pQueue.add("C++");
pQueue.add("Java");
pQueue.add("Python");
// Printing the most priority element
System.out.println("Head value using peek function:"
+ pQueue.peek());
// Printing all elements
System.out.println("The queue elements:");
Iterator itr = pQueue.iterator();
while (itr.hasNext())
System.out.println(itr.next());
// Removing the top priority element (or head) and
// printing the modified pQueue using poll()
pQueue.poll();
System.out.println("After removing an element" +
"with poll function:");
Iterator<String> itr2 = pQueue.iterator();
while (itr2.hasNext())
System.out.println(itr2.next());
// Removing Java using remove()
pQueue.remove("Java");
System.out.println("after removing Java with" +
" remove function:");
Iterator<String> itr3 = pQueue.iterator();
while (itr3.hasNext())
System.out.println(itr3.next());
// Check if an element is present using contains()
boolean b = pQueue.contains("C");
System.out.println ( "Priority queue contains C " +
"or not?: " + b);
// Getting objects from the queue using toArray()
// in an array and print the array
Object[] arr = pQueue.toArray();
System.out.println ( "Value in array: ");
for (int i = 0; i<arr.length; i++)
System.out.println ( "Value: " + arr[i].toString()) ;
}
}
运行结果如下
➜ queue git:(master) ✗ java Example
Head value using peek function:C
The queue elements:
C
C++
Java
Python
After removing an elementwith poll function:
C++
Python
Java
after removing Java with remove function:
C++
Python
Priority queue contains C or not?: false
Value in array:
Value: C++
Value: Python
boolean add(E element)
: This method inserts the specified element into this priority queue.public remove()
: This method removes a single instance of the specified element from this queue, if it is presentpublic poll()
: This method retrieves and removes the head of this queue, or returns null if this queue is empty.public peek()
: This method retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.Iterator iterator()
: Returns an iterator over the elements in this queue.boolean contains(Object o)
: This method returns true if this queue contains the specified elementvoid clear()
: This method is used to remove all of the contents of the priority queue.boolean offer(E e)
: This method is used to insert a specific element into the priority queue.int size()
: The method is used to return the number of elements present in the set.toArray()
: This method is used to return an array containing all of the elements in this queue.Comparator comparator()
: The method is used to return the comparator that can be used to order the elements of the queue.