java PriorityQueueTest.java

简介

优先级队列内部采用大顶堆或者小顶堆实现

code

 import java.util.*;
 import java.time.*;
public class PriorityQueueTest {
    public static void main(String[] args) {
        PriorityQueue<LocalDate> pq = new PriorityQueue<>();
        pq.add(LocalDate.of(1906, 12, 9));
        pq.add(LocalDate.of(1815, 12, 10));
        pq.add(LocalDate.of(1903, 12, 3));
        pq.add(LocalDate.of(1910, 6, 22));

        System.out.println("Iterating over elements...");
        for(LocalDate date : pq)
            System.out.println(date);
        System.out.println("Removing elements...");
        while(!pq.isEmpty())
            System.out.println(pq.remove());

    }
}

result

Iterating over elements...
1815-12-10
1906-12-09
1903-12-03
1910-06-22
Removing elements...
1815-12-10
1903-12-03
1906-12-09
1910-06-22

总结

遍历的时候确实有一种随机的现象。但是删除输出的时候就是以最小顺序实现。

Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
原文地址:https://www.cnblogs.com/eat-too-much/p/13887547.html