Java优先级队列

package com.lk.A;

import java.util.PriorityQueue;

public class Test5 {
    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
        for(int i=5;i>0;i--){
            queue.offer(i);
        }
        while(!queue.isEmpty()){
            System.out.print(queue.remove()+" ");
        }
    }
}

 

1 2 3 4 5 

 

PriorityQueue是优先级队列,它可以将队列中的元素进行排序。排序的方式可以使自然顺序,也可以指定排序方式。对于自然数而言,默认使用升序排序。需要注意的是,如果需要按照顺序取出元素,需要使用remove()方法。

posted @ 2015-04-07 19:00  luankun0214  阅读(188)  评论(0编辑  收藏  举报