Java Queue 接口

Java 的 集合框架 中的 Queue 接口提供了队列数据结构的功能。它扩展了 Collection 接口。


实现 Queue 的类

由于 Queue 是一个 接口,我们无法直接实现它。

为了使用 Queue 的功能,我们需要使用实现它的类

ArrayDeque, LinkedList and PriorityQueue implements the Queue interface in Java.

扩展 Queue 的接口

Queue 接口也被各种子接口扩展

  • 双端队列
  • BlockingQueue
  • BlockingDeque
Deque, BlockingQueue and BlockingDeque extends the the Queue interface.

队列数据结构的工作原理

在队列中,元素以先进先出的方式存储和访问。也就是说,元素是从后端添加,从前端移除

Working of queue data structure: first in first out.

如何使用 Queue?

在 Java 中,我们必须导入 java.util.Queue 包才能使用 Queue

// LinkedList implementation of Queue
Queue<String> animal1 = new LinkedList<>();

// Array implementation of Queue
Queue<String> animal2 = new ArrayDeque<>();

// Priority Queue implementation of Queue
Queue<String> animal3 = new PriorityQueue<>();

在这里,我们分别创建了 LinkedListArrayDequePriorityQueue 类的对象 animal1animal2animal3。这些对象可以使用 Queue 接口的功能。


Queue 的方法

Queue 接口包含 Collection 接口的所有方法。这是因为 CollectionQueue 的超接口。

Queue 接口的一些常用方法是

  • add() - 将指定的元素插入队列。如果任务成功,add() 返回 true,否则抛出 异常
  • offer() - 将指定的元素插入队列。如果任务成功,offer() 返回 true,否则返回 false
  • element() - 返回队列的头部。如果队列为空,则抛出异常。
  • peek() - 返回队列的头部。如果队列为空,则返回 null
  • remove() - 返回并移除队列的头部。如果队列为空,则抛出异常。
  • poll() - 返回并移除队列的头部。如果队列为空,则返回 null

Queue 接口的实现

1. 实现 LinkedList 类

import java.util.Queue;
import java.util.LinkedList;

class Main {

    public static void main(String[] args) {
        // Creating Queue using the LinkedList class
        Queue<Integer> numbers = new LinkedList<>();

        // offer elements to the Queue
        numbers.offer(1);
        numbers.offer(2);
        numbers.offer(3);
        System.out.println("Queue: " + numbers);

        // Access elements of the Queue
        int accessedNumber = numbers.peek();
        System.out.println("Accessed Element: " + accessedNumber);

        // Remove elements from the Queue
        int removedNumber = numbers.poll();
        System.out.println("Removed Element: " + removedNumber);

        System.out.println("Updated Queue: " + numbers);
    }
}

输出

Queue: [1, 2, 3]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 3]

要了解更多,请访问 Java LinkedList。

2. 实现 PriorityQueue 类

import java.util.Queue;
import java.util.PriorityQueue;

class Main {

    public static void main(String[] args) {
        // Creating Queue using the PriorityQueue class
        Queue<Integer> numbers = new PriorityQueue<>();

        // offer elements to the Queue
        numbers.offer(5);
        numbers.offer(1);
        numbers.offer(2);
        System.out.println("Queue: " + numbers);

        // Access elements of the Queue
        int accessedNumber = numbers.peek();
        System.out.println("Accessed Element: " + accessedNumber);

        // Remove elements from the Queue
        int removedNumber = numbers.poll();
        System.out.println("Removed Element: " + removedNumber);

        System.out.println("Updated Queue: " + numbers);
    }
}

输出

Queue: [1, 5, 2]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 5]

要了解更多,请访问 Java PriorityQueue


在接下来的教程中,我们将详细学习 Queue 接口的不同子接口及其实现。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战