Java PriorityQueue poll()方法

java.util.PriorityQueue.poll() 方法用于检索和删除队列的开头,如果队列为空,则返回null。

1 语法

public E poll()	

2 参数

3 返回值

返回队列的开头,如果队列为空,则返回null。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.PriorityQueue.poll()方法的例子
 */
import java.util.*;

public class Demo {
    public static void main(String args[]) {

        // create priority queue
        PriorityQueue < Integer >  prq = new PriorityQueue < Integer > ();

        // insert values in the queue
        for ( int i = 3; i  <  10; i++ ) {
            prq.add (new Integer (i)) ;
        }

        System.out.println("Initial priority queue values are: "+ prq);

        // get the head from the queue
        Integer head = prq.poll();

        System.out.println("Head of the queue is: "+ head);
        System.out.println("Priority queue values after poll: "+ prq);
    }
}

输出结果为:

Initial priority queue values are: [3, 4, 5, 6, 7, 8, 9]
Head of the queue is: 3
Priority queue values after poll: [4, 6, 5, 9, 7, 8]

热门文章

优秀文章