Appearance
Java队列接口
Java集合框架的Queue
接口提供了队列数据结构的功能,它是Collection
接口的扩展。
实现Queue的类
由于Queue
是一个接口,所以不能直接实现它。
为了使用Queue
的功能,我们需要使用实现它的类:
扩展Queue的接口
Queue
接口还被多个子接口扩展:
Deque
BlockingQueue
BlockingDeque
队列数据结构的工作原理
在队列中,元素按照先进先出(FIFO)的方式存储和访问。也就是说,元素从后面添加,从前面移除。
如何使用Queue?
在Java中,我们必须导入java.util.Queue
包才能使用Queue
。
java
// LinkedList实现的Queue
Queue<String> animal1 = new LinkedList<>();
// Array实现的Queue
Queue<String> animal2 = new ArrayDeque<>();
// PriorityQueue实现的Queue
Queue<String> animal 3 = new PriorityQueue<>();
在这里,我们创建了LinkedList
、ArrayDeque
和PriorityQueue
类的对象animal1、animal2和animal3。这些对象可以使用Queue
接口的功能。
Queue的方法
Queue
接口包括了Collection
接口的所有方法。这是因为Collection
是Queue
的超级接口。
Queue
接口的一些常用方法包括:
- add() - 将指定元素插入队列。如果任务成功,
add()
返回true
;否则,抛出异常。 - offer() - 将指定元素插入队列。如果任务成功,
offer()
返回true
;否则,返回false
。 - element() - 返回队列的头部。如果队列为空,抛出异常。
- peek() - 返回队列的头部。如果队列为空,返回
null
。 - remove() - 返回并移除队列的头部。如果队列为空,抛出异常。
- poll() - 返回并移除队列的头部。如果队列为空,返回
null
。
Queue接口的实现
1. 实现LinkedList类
java
import java.util.Queue;
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
// 使用LinkedList类创建Queue
Queue<Integer> numbers = new LinkedList<>();
// 向队列添加元素
numbers.offer(1);
numbers.offer(2);
numbers.offer(3);
System.out.println("Queue: " + numbers);
// 访问队列的元素
int accessedNumber = numbers.peek();
System.out.println("Accessed Element: " + accessedNumber);
// 从队列中移除元素
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类
java
import java.util.Queue;
import java.util.PriorityQueue;
class Main {
public static void main(String[] args) {
// 使用PriorityQueue