当前位置:首页 > Java

队列如何使用java

2026-03-24 06:29:33Java

队列的基本概念

队列是一种先进先出(FIFO)的数据结构,元素从队尾入队,从队头出队。Java提供了多种实现队列的方式,包括LinkedListArrayDequePriorityQueue等。

使用LinkedList实现队列

LinkedList实现了Queue接口,可以直接作为队列使用。

队列如何使用java

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

public class QueueExample {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();

        // 入队操作
        queue.add(10);
        queue.add(20);
        queue.add(30);

        // 出队操作
        int firstElement = queue.remove();
        System.out.println("出队元素: " + firstElement);

        // 查看队头元素
        int peekElement = queue.peek();
        System.out.println("队头元素: " + peekElement);

        // 判断队列是否为空
        boolean isEmpty = queue.isEmpty();
        System.out.println("队列是否为空: " + isEmpty);
    }
}

使用ArrayDeque实现队列

ArrayDeque是双端队列,也可以作为普通队列使用,性能通常优于LinkedList

队列如何使用java

import java.util.ArrayDeque;
import java.util.Queue;

public class ArrayDequeExample {
    public static void main(String[] args) {
        Queue<String> queue = new ArrayDeque<>();

        // 入队操作
        queue.offer("Apple");
        queue.offer("Banana");
        queue.offer("Cherry");

        // 出队操作
        String fruit = queue.poll();
        System.out.println("出队元素: " + fruit);

        // 查看队头元素
        String nextFruit = queue.peek();
        System.out.println("队头元素: " + nextFruit);
    }
}

使用PriorityQueue实现优先队列

PriorityQueue是一个基于优先级的队列,元素按自然顺序或自定义比较器排序。

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

public class PriorityQueueExample {
    public static void main(String[] args) {
        Queue<Integer> priorityQueue = new PriorityQueue<>();

        // 入队操作
        priorityQueue.add(50);
        priorityQueue.add(10);
        priorityQueue.add(30);

        // 出队操作(按优先级顺序)
        while (!priorityQueue.isEmpty()) {
            System.out.println("出队元素: " + priorityQueue.poll());
        }
    }
}

自定义队列实现

如果需要完全控制队列行为,可以手动实现队列接口。

public class CustomQueue<T> {
    private static final int DEFAULT_CAPACITY = 10;
    private Object[] elements;
    private int front;
    private int rear;
    private int size;

    public CustomQueue() {
        elements = new Object[DEFAULT_CAPACITY];
        front = 0;
        rear = -1;
        size = 0;
    }

    public void enqueue(T item) {
        if (size == elements.length) {
            resize();
        }
        rear = (rear + 1) % elements.length;
        elements[rear] = item;
        size++;
    }

    public T dequeue() {
        if (isEmpty()) {
            throw new IllegalStateException("队列为空");
        }
        T item = (T) elements[front];
        front = (front + 1) % elements.length;
        size--;
        return item;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private void resize() {
        Object[] newArray = new Object[elements.length * 2];
        for (int i = 0; i < size; i++) {
            newArray[i] = elements[(front + i) % elements.length];
        }
        elements = newArray;
        front = 0;
        rear = size - 1;
    }
}

注意事项

  • add()remove()方法在队列满或空时会抛出异常,而offer()poll()返回特殊值(falsenull)。
  • PriorityQueue的排序规则可以通过Comparator自定义。
  • 多线程环境下应使用ConcurrentLinkedQueueBlockingQueue实现线程安全。

分享给朋友:

相关文章

Vue消息队列实现

Vue消息队列实现

Vue 消息队列实现方法 在 Vue 中实现消息队列可以通过多种方式,以下是几种常见的方法: 使用 Vuex 状态管理 Vuex 可以用于管理全局状态,适合实现消息队列功能。通过 mutations…

react如何使用函数

react如何使用函数

使用函数组件的基本语法 在React中,函数组件是通过JavaScript函数定义的组件。函数接收props作为参数,并返回React元素。 function Welcome(props) {…

php实现双向队列

php实现双向队列

PHP 实现双向队列的方法 双向队列(Deque,Double-ended Queue)是一种允许在队列两端进行插入和删除操作的数据结构。PHP 中可以通过数组或 SplDoublyLinkedLis…

react如何使用axios

react如何使用axios

安装 Axios 在 React 项目中安装 Axios,可以通过 npm 或 yarn 进行安装: npm install axios 或 yarn add axios 导入 Axios 在需要…

react如何使用图片

react如何使用图片

在React中使用图片的方法 静态图片导入 在React组件中可以直接通过import导入图片文件,这种方式适用于已知的静态资源。导入后,图片会被处理为模块,可通过src属性引用。 import R…

react如何使用插槽

react如何使用插槽

使用props.children实现基础插槽 React中插槽的核心是通过props.children传递子组件。父组件在标签内部放置的内容会自动成为children属性: // 父组件 <C…