Implementing Queues in Java: Array, Linked List, and List Approaches
This article explains the concept of FIFO queues, outlines their key properties, and provides three Java implementations—using an array, a linked list, and a List—complete with source code, usage examples, and typical application scenarios.
Queues are logical data structures that follow the First In First Out (FIFO) principle, featuring two main operations: enqueue (insert) and dequeue (remove). The article first revisits the queue concept and its characteristics, then presents three concrete Java implementations.
1. Custom Queue using an Array
public class MyQueue
{
private Object[] queue; // storage container
private int head; // head pointer
private int tail; // tail pointer
private int size; // actual stored length
private int maxSize; // maximum capacity
public MyQueue() {
this.maxSize = 10;
this.head = 0;
this.tail = -1;
this.size = 0;
this.queue = new Object[this.maxSize];
}
public MyQueue(int initSize) {
this.maxSize = initSize;
this.head = 0;
this.tail = -1;
this.size = 0;
this.queue = new Object[this.maxSize];
}
public E peek() throws Exception {
if (size == 0) {
throw new Exception("队列中暂无数据");
}
return (E) this.queue[this.head];
}
public boolean offer(E e) throws Exception {
if (tail >= (maxSize - 1)) {
throw new Exception("添加失败,队列已满");
}
this.queue[++tail] = e;
size++;
return true;
}
public E poll() throws Exception {
if (size == 0) {
throw new Exception("删除失败,队列为空");
}
size--;
return (E) this.queue[head++];
}
public static void main(String[] args) throws Exception {
MyQueue queue = new MyQueue();
queue.offer("Hello");
queue.offer("Java");
System.out.println(queue.peek());
queue.poll();
System.out.println(queue.poll());
}
}The above code prints:
Hello
Java2. Custom Queue using a Linked List
public class QueueByLinked
{
static class Node
{
E item; // current value
Node
next; // next node
Node(E e) { this.item = e; }
}
private Node firstNode; // head element
private Node lastNode; // tail element
private int size; // actual stored count
private int maxSize; // maximum capacity
public QueueByLinked(int maxSize) {
if (maxSize <= 0) throw new RuntimeException("队列最大容量不能为空");
firstNode = lastNode = new Node(null);
this.size = 0;
this.maxSize = maxSize;
}
public boolean isEmpty() { return size == 0; }
public void offer(Object e) {
if (maxSize <= size) throw new RuntimeException("队列已满");
Node node = new Node(e);
lastNode = lastNode.next = node;
size++;
}
public Node poll() {
if (isEmpty()) throw new RuntimeException("队列为空");
size--;
return firstNode = firstNode.next;
}
public Node peek() {
if (isEmpty()) throw new RuntimeException("队列为空");
return firstNode.next;
}
public static void main(String[] args) {
QueueByLinked queue = new QueueByLinked(10);
queue.offer("Hello");
queue.offer("JDK");
queue.offer("Java");
System.out.println(queue.poll().item);
System.out.println(queue.poll().item);
System.out.println(queue.poll().item);
}
}The execution output is:
Hello
JDK
Java3. Queue implemented with List
import java.util.ArrayList;
import java.util.List;
public class QueueByList
{
private List value; // storage container
public QueueByList() {
value = new ArrayList();
}
public boolean isEmpty() { return value.size() == 0; }
public void offer(Object e) { value.add(e); }
public E poll() {
if (isEmpty()) throw new RuntimeException("队列为空");
E item = (E) value.get(0);
value.remove(0);
return item;
}
public E peek() {
if (isEmpty()) throw new RuntimeException("队列为空");
return (E) value.get(0);
}
public static void main(String[] args) {
QueueByList queue = new QueueByList();
queue.offer("Hello");
queue.offer("JDK");
queue.offer("Java");
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
}
}Running this code also prints:
Hello
JDK
JavaQueue Usage Scenarios
Storing tasks waiting to be executed in multithreaded environments.
Holding threads waiting for a fair lock in concurrency control.
Message‑oriented middleware task queues.
Summary
All three implementations demonstrate that any container can serve as a queue as long as it preserves FIFO order and provides the core operations—enqueue, dequeue, emptiness check, and peek—thereby constituting a custom queue implementation.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.