Java
Queue
Programming
Code Examples
Java Collections

How do I instantiate a Queue object in java?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In Java, Queue is an interface, so you do not instantiate it directly. Instead, you create an object from a class that implements Queue, such as ArrayDeque, LinkedList, or PriorityQueue. The best implementation depends on whether you want normal FIFO behavior, ordering by priority, or deque-like operations.

Instantiate a FIFO Queue

For a normal first-in, first-out queue, ArrayDeque is usually the best default:

java
1import java.util.ArrayDeque;
2import java.util.Queue;
3
4public class Main {
5    public static void main(String[] args) {
6        Queue<Integer> queue = new ArrayDeque<>();
7
8        queue.add(10);
9        queue.add(20);
10        queue.add(30);
11
12        System.out.println(queue.remove()); // 10
13        System.out.println(queue.peek());   // 20
14    }
15}

This is the standard pattern:

  • declare the variable as the interface type Queue
  • instantiate a concrete implementation such as ArrayDeque

That keeps the code flexible while still giving you a real working object.

Why ArrayDeque Is Often Better Than LinkedList

A lot of older examples use LinkedList as a queue:

java
Queue<String> queue = new LinkedList<>();

That works, but for a plain queue, ArrayDeque is usually preferred. It is designed specifically for efficient queue and deque operations and usually performs better than LinkedList for this role.

So unless you specifically need linked-list behavior for another reason, ArrayDeque is the better default answer in modern Java code.

Use PriorityQueue for Ordered Retrieval

If you want the smallest element to come out first instead of strict insertion order, use PriorityQueue:

java
1import java.util.PriorityQueue;
2import java.util.Queue;
3
4public class Main {
5    public static void main(String[] args) {
6        Queue<Integer> queue = new PriorityQueue<>();
7
8        queue.add(30);
9        queue.add(10);
10        queue.add(20);
11
12        System.out.println(queue.remove()); // 10
13        System.out.println(queue.remove()); // 20
14    }
15}

This is still a Queue, but it does not behave like a FIFO queue. The retrieval order is based on priority, not insertion order.

That difference matters because many bugs come from picking the right interface but the wrong implementation.

Choose Between add and offer

Once you have a queue, you also need to pick the insertion method. add may throw an exception on insertion failure, while offer is designed for queue-style insertion and can return false instead.

For most queue code, offer, poll, and peek express the intent more clearly than add, remove, and element.

Common Queue Operations

Once you have a queue, the main methods are:

  • 'add or offer to insert'
  • 'remove or poll to take an element out'
  • 'element or peek to inspect the front element'

A small example:

java
1Queue<String> queue = new ArrayDeque<>();
2queue.offer("A");
3queue.offer("B");
4
5System.out.println(queue.peek()); // A
6System.out.println(queue.poll()); // A
7System.out.println(queue.poll()); // B
8System.out.println(queue.poll()); // null

The offer, poll, and peek methods are often safer because they use special return values instead of throwing exceptions in empty-queue situations.

Generic Types Matter

Always specify the element type of the queue:

java
Queue<String> names = new ArrayDeque<>();

This gives you compile-time type safety and avoids raw-type warnings. In older Java examples you may see raw collections, but modern code should use generics consistently.

Common Pitfalls

One common mistake is trying to instantiate Queue directly, such as new Queue(). That fails because Queue is an interface, not a concrete class.

Another mistake is using PriorityQueue when FIFO behavior was expected. A priority queue removes items by ordering, not by insertion order.

Developers also sometimes default to LinkedList out of habit when ArrayDeque is usually the better queue implementation.

Finally, do not forget the difference between remove and poll. remove throws if the queue is empty, while poll returns null.

Summary

  • 'Queue is an interface, so instantiate an implementing class such as ArrayDeque.'
  • 'ArrayDeque is usually the best default for a normal FIFO queue.'
  • 'PriorityQueue is a queue too, but it orders by priority rather than insertion order.'
  • Use generics so the queue has a clear element type.
  • Pick the implementation based on the behavior you actually need, not just the interface name.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.