Java Java.util.PriorityQueue类
它是一个基于优先级堆的优先级队列。
- 这个类中的元素是按自然顺序排列的,或者取决于我们在构造时使用的构造器。
- 它不允许空指针。
- 如果它依赖于自然排序,它不允许插入一个不可比的对象。
构造函数
- PriorityQueue(): 创建一个具有默认初始容量(11)的PriorityQueue,根据其元素的自然排序来排序。
- PriorityQueue(Collection c): 创建一个包含指定集合中元素的PriorityQueue。
- PriorityQueue(int initialCapacity) : 以指定的初始容量创建一个PriorityQueue,按照元素的自然顺序排列。
- PriorityQueue(int initialCapacity, Comparator comparator): 创建一个具有指定初始容量的PriorityQueue,根据指定的比较器对其元素进行排序。
- PriorityQueue(PriorityQueue c) : 创建一个PriorityQueue,包含指定优先级队列中的元素。
- PriorityQueue(SortedSet c) : 创建一个PriorityQueue,包含指定排序集的元素。
声明:
public class PriorityQueue
extends AbstractQueue
implements Serializable
方法
- add(element) : java.util.PriorityQueue.add()将元素插入到优先队列中。
语法 :
public boolean add(E e)
参数 :
element:我们需要添加的元素。
返回 :
调用返回true。
异常 :
-> ClassCastException
-> NullPointerException
- comparator() : java.util.PriorityQueue.comparator()对队列中的元素排序。
语法 :
public comparator comparator()
返回 :
对队列进行排序,如果是自然排序,则返回null。 -
contains(Object obj) : java.util.PriorityQueue.contains(obj) 如果优先级队列包含 “obj “这个元素,则返回true。
语法 :
public boolean contains(Object obj)
参数 :
obj:要检查的对象
返回 :
true – 如果该对象存在,否则,返回false -
iterator() :java.util.PriorityQueue.iterator() 迭代队列元素。
语法 :
public Iterator iterator()
返回 :
在队列中的元素上调用迭代器。 -
offer(element) :java.util.PriorityQueue.offer()需要向给定的优先级队列插入一个特定的元素。
语法 :
public boolean offer(E element)
参数 :
element:要输入的特定元素。
返回 :
调用返回true。
Exception :
-> ClassCastException
-> NullPointerException
- peek() : java.util.PriorityQueue.peek()识别队列的头部元素。
语法 :
public E peek()
返回 :
如果头部存在则调用,否则为空 -
poll() :java.util.PriorityQueue.poll()识别头部,然后将其删除。
语法 :
public E poll()返回 :
如果头部存在则调用,否则为空 -
remove(Object obj) : java.util.PriorityQueue.remove()从队列中删除一个特定的对象。
语法 :
public boolean remove(Object obj)
参数 :
obj:要移除的对象
返回 :
true – 如果obj被移除
- size() :java.util.PriorityQueue.size()返回优先队列中元素的大小。
语法 :
public int size()
返回 :
元素的数量 -
toArray() : java.util.PriorityQueue.toArray()返回一个包含PriorityQueue元素的数组。
语法 :
public Object[] toArray()
返回 :
返回一个包含PriorityQueue所有元素的数组。
- toArray(T[] array) :java.util.PriorityQueue.toArray(T[] a)返回具有优先级队列元素的数组。
语法 :
public T[] toArray(T[] array)
参数 :
array:要被排序的数组。
返回 :
调用一个包含该数组所有元素的数组。
异常 :
-> ArrayStoreException
-> NullPointerException -
clear() : java.util.PriorityQueue.clear()清除PriorityQueue的所有元素。
语法 :
public void clear()
// Java Program illustrating the methods
// of java.utl.priorityQueue class
// add(), comparator(), conatins(), iterator(), offer()
// peek(), poll(), toArray(), size(), toArray(t[] g1),
// remove(), clear()
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
// Creating a Priority Queue :
PriorityQueue <Integer> geek = new PriorityQueue <Integer> ();
for(int i=2; i<=20; i=i+2)
{
// Use of add() :
geek.add(new Integer (i));
}
System.out.println("geek PriorityQueue : " + geek);
// Use of comparator()
// No ordering is required here as it is naturally ordered.
Comparator geek_comp = geek.comparator();
System.out.println("geek PriorityQueue : " + geek_comp);
// Use of contains()
boolean check = geek.contains(6);
System.out.println("Use of contains() : " + check);
// Use of iterator()
Iterator g_iterator = geek.iterator();
System.out.print("Iterator values : ");
while(g_iterator.hasNext())
{
System.out.print(g_iterator.next() + " ");
}
System.out.println("");
// Use of offer()
geek.offer(3050);
System.out.println("geek PriorityQueue : " + geek);
// Use of peek()
System.out.println("Head of PriorityQueue via peek : " + geek.peek());
//Use of poll()
int h = geek.poll();
System.out.println("\nHead of PriorityQueue via poll : " + h);
System.out.println("geek PriorityQueue bcz of poll() : " + geek);
// Use of remove()
boolean r = geek.remove(8);
System.out.println("\nCan remove : " + r);
System.out.println("geek PriorityQueue bcz of remove() : " + geek);
// use of size()
System.out.println("\nSize of PriorityQueue : " + geek.size());
// Use of toArray()
Object[] g = geek.toArray();
System.out.print ( "Array from PriorityQueue : ");
for ( int i = 0; i<g.length; i++ )
{
System.out.print (g[i].toString() + " ") ;
}
System.out.println("\n");
// Use of toArray(t[] g1) :
Integer[] g2 = new Integer[5];
Integer[] g1 = geek.toArray(g2);
System.out.print ( "Array from PriorityQueue of size 5 : ");
for ( int i = 0; i<g1.length; i++ )
{
System.out.print (g1[i].toString() + " ") ;
}
System.out.println("\n");
// Use of clear()
geek.clear();
System.out.println("PriorityQueue after clear() : " + geek);
}
}
输出 :
geek PriorityQueue : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
geek PriorityQueue : null
Use of contains() : true
Iterator values : 2 4 6 8 10 12 14 16 18 20
geek PriorityQueue : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3050]
Head of PriorityQueue via peek : 2
Head of PriorityQueue via poll : 2
geek PriorityQueue bcz of poll() : [4, 8, 6, 16, 10, 12, 14, 3050, 18, 20]
Can remove : true
geek PriorityQueue bcz of remove() : [4, 10, 6, 16, 20, 12, 14, 3050, 18]
Size of PriorityQueue : 9
Array from PriorityQueue : 4 10 6 16 20 12 14 3050 18
Array from PriorityQueue of size 5 : 4 10 6 16 20 12 14 3050 18
PriorityQueue after clear() : []
极客教程