Java——PriorityQueue用法

Java——PriorityQueue用法

PriorityQueue 是 Java 中基于优先级堆实现的一个队列,可以用来存储一组元素,并按照一定的优先级顺序访问这些元素。其中,优先级高的元素会被先访问。

PriorityQueue 类位于 java.util 包中,是一个泛型类,可以存储任意类型的对象。

在 PriorityQueue 内部,元素会被排序,排序规则由元素的自然顺序或者指定的 Comparator 决定。

PriorityQueue 支持以下操作:

  • offer(E e):将指定元素插入到队列中。
  • peek():获取队列头部的元素,但不删除该元素。如果队列为空,则返回 null。
  • poll():获取并删除队列头部的元素。如果队列为空,则返回 null。
  • remove(Object o):从队列中删除指定元素。
  • size():获取队列中元素的数量。

下面是 PriorityQueue 的使用示例:

import java.util.PriorityQueue;

public class PriorityQueueExample {
    public static void main(String[] args) {
        // 创建一个空的 Priority Queue,默认元素按照自然顺序排序
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        // 添加元素到 Priority Queue
        pq.offer(5);
        pq.offer(3);
        pq.offer(8);

        // 获取并删除队列头部的元素,按照优先级顺序输出
        while (!pq.isEmpty()) {
            System.out.println(pq.poll()); // 输出结果:3 5 8
        }

        // 创建一个自定义排序规则的 Priority Queue
        PriorityQueue<String> pq2 = new PriorityQueue<>(
                (s1, s2) -> s2.length() - s1.length()); // 按照字符串长度降序排序

        // 添加元素到 Priority Queue
        pq2.offer("apple");
        pq2.offer("banana");
        pq2.offer("cherry");

        // 获取并删除队列头部的元素,按照自定义排序规则输出
        while (!pq2.isEmpty()) {
            System.out.println(pq2.poll()); // 输出结果:banana cherry apple
        }
    }
}

当使用 PriorityQueue 时,可以通过自然顺序或自定义 Comparator 来定义元素的排序规则。以下是一些示例:

1. 使用自然顺序排序的示例:

import java.util.PriorityQueue;

public class NaturalOrderExample {
    public static void main(String[] args) {
        // 使用自然顺序排序(整数)
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        pq.offer(5);
        pq.offer(3);
        pq.offer(8);

        while (!pq.isEmpty()) {
            System.out.println(pq.poll()); // 输出结果:3 5 8
        }
    }
}

2. 使用自定义 Comparator 定义排序规则的示例:

import java.util.Comparator;
import java.util.PriorityQueue;

public class CustomComparatorExample {
    public static void main(String[] args) {
        // 使用自定义的 Comparator 来定义排序规则(字符串长度降序)
        PriorityQueue<String> pq = new PriorityQueue<>(
                Comparator.comparingInt(String::length).reversed());

        pq.offer("apple");
        pq.offer("banana");
        pq.offer("cherry");

        while (!pq.isEmpty()) {
            System.out.println(pq.poll()); // 输出结果:banana cherry apple
        }
    }
}

3. 使用自定义对象和 Comparator 示例:

import java.util.Comparator;
import java.util.PriorityQueue;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

public class CustomObjectComparatorExample {
    public static void main(String[] args) {
        // 使用自定义的 Comparator 来定义排序规则(按照年龄升序)
        PriorityQueue<Person> pq = new PriorityQueue<>(Comparator.comparingInt(Person::getAge));

        pq.offer(new Person("Alice", 25));
        pq.offer(new Person("Bob", 30));
        pq.offer(new Person("Charlie", 20));

        while (!pq.isEmpty()) {
            Person person = pq.poll();
            System.out.println(person.getName() + " - " + person.getAge());
        }
    }
}

当我们需要将 PriorityQueue 定义为最大堆时,可以使用 Collections.reverseOrder() 方法来反转元素的自然顺序或者指定的 Comparator,从而实现最大堆的效果。以下是一个示例:

import java.util.Collections;
import java.util.PriorityQueue;

public class MaxHeapExample {
    public static void main(String[] args) {
        // 使用 Collections.reverseOrder() 反转自然顺序,定义 PriorityQueue 为最大堆
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

        maxHeap.offer(5);
        maxHeap.offer(3);
        maxHeap.offer(8);

        while (!maxHeap.isEmpty()) {
            System.out.println(maxHeap.poll()); // 输出结果:8 5 3
        }
    }
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喻师傅

谢谢您!我会继续努力创作!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值