Cola de prioridad en Java

Referencia: https://www.cnblogs.com/wei-jing/p/10806236.html

//匿名内部类的使用
    Comparator<Integer> cm = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    };
    Queue<Integer> queue = new PriorityQueue<>(cm);
    queue.offer(1);
    queue.offer(3);
    queue.offer(2);
    queue.offer(6);
    queue.offer(-4);
    
    System.out.println(queue.poll());
    System.out.println(queue.poll());
    System.out.println(queue.poll());
    System.out.println(queue.poll());
    System.out.println(queue.poll());

 

Supongo que te gusta

Origin blog.csdn.net/mugeit/article/details/114526263
Recomendado
Clasificación