有界的优先队列 BoundedPriorityQueue

BoundedPriorityQueue 

导入maven 依赖

 <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>4.0.12</version>
    </dependency>

测试

public static void main(String[] args) {
        /**
         * 创建3个节点的小顶堆
         */
        Queue<Integer> boundedPriorityQueue = new BoundedPriorityQueue<>(3, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        boundedPriorityQueue.add(10);
        boundedPriorityQueue.add(20);
        boundedPriorityQueue.add(30);
        boundedPriorityQueue.add(40);
        System.out.println(boundedPriorityQueue);
        System.out.println(boundedPriorityQueue.poll());
        boundedPriorityQueue.add(33);
        boundedPriorityQueue.add(35);
        System.out.println(boundedPriorityQueue);

    }

猜你喜欢

转载自blog.csdn.net/u011243684/article/details/87916581