您好,登录后才能下订单哦!
# Java队列数据结构的实现方法是什么
## 1. 队列数据结构概述
队列(Queue)是一种先进先出(FIFO, First-In-First-Out)的线性数据结构,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。队列在计算机科学中应用广泛,如操作系统的进程调度、打印任务管理、消息队列系统等。
### 1.1 队列的基本特性
- **先进先出原则**:最先进入队列的元素将最先被移除
- **两个主要操作**:
- 入队(enqueue):在队列尾部添加元素
- 出队(dequeue):从队列头部移除元素
- **辅助操作**:
- peek:查看队首元素但不移除
- isEmpty:判断队列是否为空
- size:获取队列中元素数量
### 1.2 队列的分类
1. **普通队列**:基本FIFO结构
2. **双端队列(Deque)**:两端都可进行入队和出队操作
3. **优先队列(PriorityQueue)**:元素按优先级出队
4. **循环队列**:将队列存储空间的最后一个位置绕回第一个位置
## 2. Java中的队列接口
Java集合框架提供了`Queue`接口(位于`java.util`包中),定义了队列的基本操作:
```java
public interface Queue<E> extends Collection<E> {
boolean add(E e); // 添加元素到队列,失败时抛出异常
boolean offer(E e); // 添加元素到队列,失败时返回false
E remove(); // 移除并返回队首元素,队列空时抛出异常
E poll(); // 移除并返回队首元素,队列空时返回null
E element(); // 返回队首元素但不移除,队列空时抛出异常
E peek(); // 返回队首元素但不移除,队列空时返回null
}
LinkedList
实现了Queue
接口,可以作为普通队列使用:
import java.util.LinkedList;
import java.util.Queue;
public class LinkedListQueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// 入队操作
queue.add("A");
queue.offer("B");
// 查看队首
System.out.println("队首元素: " + queue.peek());
// 出队操作
String element = queue.remove();
System.out.println("出队元素: " + element);
// 遍历队列
System.out.println("队列元素:");
queue.forEach(System.out::println);
}
}
特点: - 基于双向链表实现 - 无容量限制 - 插入和删除操作时间复杂度为O(1) - 适合频繁插入删除但随机访问较少的场景
ArrayDeque
是基于数组的双端队列实现,也可作为普通队列使用:
import java.util.ArrayDeque;
import java.util.Queue;
public class ArrayDequeQueueExample {
public static void main(String[] args) {
Queue<Integer> queue = new ArrayDeque<>(5);
// 入队操作
for (int i = 1; i <= 5; i++) {
queue.offer(i * 10);
}
// 尝试添加超出初始容量
System.out.println("添加60结果: " + queue.offer(60));
// 出队操作
while (!queue.isEmpty()) {
System.out.println("出队: " + queue.poll());
}
}
}
特点: - 基于可扩容循环数组实现 - 初始默认容量16,可按需增长 - 比LinkedList更节省内存 - 大多数操作时间复杂度为O(1) - 性能通常优于LinkedList
优先队列,元素按自然顺序或Comparator指定的顺序出队:
import java.util.PriorityQueue;
import java.util.Queue;
public class PriorityQueueExample {
public static void main(String[] args) {
// 自然顺序(最小堆)
Queue<Integer> minHeap = new PriorityQueue<>();
minHeap.offer(30);
minHeap.offer(10);
minHeap.offer(20);
System.out.println("最小优先队列:");
while (!minHeap.isEmpty()) {
System.out.println(minHeap.poll());
}
// 自定义Comparator(最大堆)
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
maxHeap.offer(30);
maxHeap.offer(10);
maxHeap.offer(20);
System.out.println("\n最大优先队列:");
while (!maxHeap.isEmpty()) {
System.out.println(maxHeap.poll());
}
}
}
特点: - 基于优先级堆(通常是小顶堆)实现 - 元素排序可以是自然顺序或自定义Comparator - 入队和出队操作时间复杂度为O(log n) - 不支持null元素 - 不是线程安全的
java.util.concurrent
包提供了多种线程安全的阻塞队列实现:
有界阻塞队列,基于数组实现:
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ArrayBlockingQueueExample {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
// 生产者线程
new Thread(() -> {
try {
queue.put("消息1");
queue.put("消息2");
queue.put("消息3");
System.out.println("尝试添加第4条消息...");
queue.put("消息4"); // 将阻塞直到有空间
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
// 消费者线程
new Thread(() -> {
try {
Thread.sleep(2000);
System.out.println("取出消息: " + queue.take());
Thread.sleep(1000);
System.out.println("取出消息: " + queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
特点: - 固定大小的FIFO阻塞队列 - 支持公平性策略(可选的ReentrantLock公平性) - 适合生产者-消费者场景
可选有界的阻塞队列,基于链表实现:
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class LinkedBlockingQueueExample {
public static void main(String[] args) {
// 无界队列
BlockingQueue<String> unbounded = new LinkedBlockingQueue<>();
// 有界队列
BlockingQueue<String> bounded = new LinkedBlockingQueue<>(100);
try {
// 阻塞操作
bounded.put("项目1");
String item = bounded.take();
System.out.println("处理: " + item);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
特点: - 默认无界(Integer.MAX_VALUE),也可指定容量 - 吞吐量通常高于ArrayBlockingQueue - 比ArrayBlockingQueue使用更多内存
无界优先阻塞队列:
import java.util.concurrent.PriorityBlockingQueue;
public class PriorityBlockingQueueExample {
public static void main(String[] args) {
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
// 添加元素
queue.add(50);
queue.add(10);
queue.add(30);
try {
// 按优先级取出
System.out.println(queue.take()); // 10
System.out.println(queue.take()); // 30
System.out.println(queue.take()); // 50
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
特点: - 无界队列,会自动扩容 - 元素按自然顺序或Comparator排序 - 比PriorityQueue多了线程安全特性
实现类 | 有界性 | 锁类型 | 适用场景 |
---|---|---|---|
ArrayBlockingQueue | 有界 | 全局ReentrantLock | 固定大小的生产消费场景 |
LinkedBlockingQueue | 可选 | 双锁分离 | 高吞吐量生产消费场景 |
ConcurrentLinkedQueue | 无界 | CAS无锁 | 高并发非阻塞场景 |
PriorityBlockingQueue | 无界 | 全局ReentrantLock | 需要优先级排序的并发场景 |
public class ArrayQueue<E> {
private static final int DEFAULT_CAPACITY = 10;
private E[] elements;
private int front;
private int rear;
private int size;
@SuppressWarnings("unchecked")
public ArrayQueue() {
elements = (E[]) new Object[DEFAULT_CAPACITY];
front = 0;
rear = -1;
size = 0;
}
public boolean enqueue(E element) {
if (isFull()) {
resize();
}
rear = (rear + 1) % elements.length;
elements[rear] = element;
size++;
return true;
}
public E dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("队列为空");
}
E element = elements[front];
elements[front] = null;
front = (front + 1) % elements.length;
size--;
return element;
}
public E peek() {
if (isEmpty()) {
throw new NoSuchElementException("队列为空");
}
return elements[front];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == elements.length;
}
public int size() {
return size;
}
private void resize() {
int newCapacity = elements.length * 2;
@SuppressWarnings("unchecked")
E[] newElements = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newElements[i] = elements[(front + i) % elements.length];
}
elements = newElements;
front = 0;
rear = size - 1;
}
}
public class LinkedQueue<E> {
private static class Node<E> {
E item;
Node<E> next;
Node(E item) {
this.item = item;
}
}
private Node<E> head;
private Node<E> tail;
private int size;
public LinkedQueue() {
head = null;
tail = null;
size = 0;
}
public boolean enqueue(E element) {
Node<E> newNode = new Node<>(element);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
return true;
}
public E dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("队列为空");
}
E element = head.item;
head = head.next;
if (head == null) {
tail = null;
}
size--;
return element;
}
public E peek() {
if (isEmpty()) {
throw new NoSuchElementException("队列为空");
}
return head.item;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
}
public void bfs(Node start) {
Queue<Node> queue = new LinkedList<>();
Set<Node> visited = new HashSet<>();
queue.offer(start);
visited.add(start);
while (!queue.isEmpty()) {
Node current = queue.poll();
System.out.println("访问节点: " + current);
for (Node neighbor : current.getNeighbors()) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.offer(neighbor);
}
}
}
}
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(100);
ExecutorService threadPool = new ThreadPoolExecutor(
4, // 核心线程数
10, // 最大线程数
60, TimeUnit.SECONDS,
workQueue
);
// 提交任务
for (int i = 0; i < 50; i++) {
int taskId = i;
threadPool.execute(() -> {
System.out.println("执行任务: " + taskId);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
// 生产者
public class Producer implements Runnable {
private final BlockingQueue<Message> queue;
public Producer(BlockingQueue<Message> queue) {
this.queue = queue;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Message msg = new Message("消息-" + i);
try {
queue.put(msg);
System.out.println("生产: " + msg.getContent());
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 消费者
public class Consumer implements Runnable {
private final BlockingQueue<Message> queue;
public Consumer(BlockingQueue<Message> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
while (true) {
Message msg = queue.take();
System.out.println("消费: " + msg.getContent());
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
选择合适的队列实现:
合理设置队列容量:
避免队列成为性能瓶颈:
异常处理:
监控队列状态:
Java提供了丰富的队列实现,从基本的LinkedList到高性能的并发队列,可以满足各种场景的需求。选择正确的队列实现需要考虑以下因素:
理解各种队列实现的内部机制和适用场景,能够帮助开发者在实际应用中做出更合理的选择,构建高效稳定的系统。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。