Java Collection Queue

A Queue in Java is a FIFO (First-In-First-Out) data structure that processes elements in the order they were added. It is part of the Java Collections Framework and is useful for scenarios like task scheduling, message processing, and buffering.

Implementation

Internal Structure

Performance


Best Use Case

LinkedList

  • Elements are processed in First-In-First-Out order
  • Allows insertion and removal from both ends


  • Better for frequent mid-list insertions/deletions
  • Slower due to pointer overhead
  • Suitable for frequent insertions/deletions in the middle of the list

ArrayDeque

  • Elements are processed in First-In-First-Out order
  • Allows insertion and removal from both ends


  • Faster due to array-based storage
  • Better for stack and queue operations
  • Faster than LinkedList for queue and stack operations
  • Low memory overhead (no extra pointers)

Priority Queue

  • Elements are ordered by priority instead of insertion order
  • PriorityQueue a Min-Heap by default, meaning smallest elements are dequeued first.
  • Best for task scheduling, shortest path algorithms (like Dijkstra's algorithm), and processing elements based on priority.

Using PriorityQueue:

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

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

priorityQueue.add(30);
priorityQueue.add(10);
priorityQueue.add(20);

System.out.println(priorityQueue.poll()); // Output: 10 (Sorted order)
System.out.println(priorityQueue.poll()); // Output: 20
}
}

Try it yourself

Using LinkedList as a Queue:

LinkedList are implementations of the Deque (double-ended queue) interface

import java.util.LinkedList;
import java.util.Deque;

public class Main {
public static void main(String[] args) {
Deque<String> queue = new LinkedList<>();

queue.add("Apple");
queue.add("Banana");
queue.add("Cherry");

System.out.println(queue.poll()); // Output: Apple (FIFO)
System.out.println(queue.peek()); // Output: Banana (Still in queue)
System.out.println(queue); // Output: [Banana, Cherry]
}
}

Try it yourself

Using ArrayDeque (Double-ended Queue):

ArrayDeque are implementations of the Deque (double-ended queue) interface

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();

deque.addFirst("First");
deque.add("Middle");
deque.addLast("Last");

System.out.println(deque.pollFirst()); // Output: First
System.out.println(deque.pollLast()); // Output: Last
}
}

Try it yourself

Queue Methods:

Basic Methods of Queue Interface

Method

Description

add(E e)

Adds an element to the queue (throws exception if full)

offer(E e)

Adds an element, returns false if full

poll()

Retrieves and removes the head (returns null if empty)

remove()

Retrieves and removes the head (throws exception if empty)

peek()

Retrieves the head without removing it (returns null if empty)

element()

Retrieves the head without removing it (throws exception if empty)


Deque Methods:

All Queue Interface methods are working in Deque Interface apart from Deque have some extra methods

Method

Description

addFirst(E e)

Adds an element on first to the queue (throws exception if full)

addLast(E e)

Adds an element on last to the queue (throws exception if full)

offerFirst(E e)

Adds an element on first, returns false if full

offerLast(E e)

Adds an element on last, returns false if full

removeFirst()

Retrieves and removes on first element the head (throws exception if empty)

removeLast()

Retrieves and removes on last element the head (throws exception if empty)

pollFirst()

Retrieves and removes on first element the head (returns null if empty)

pollLast()

Retrieves and removes on last element the head (returns null if empty)

peekFirst()

Retrieves a first element the head without removing it (returns null if empty)

peekLast()

Retrieves a last element the head without removing it (returns null if empty)

getFirst()

Retrieves a first element the head without removing it (throws exception if empty)

getLast()

Retrieves a last element the head without removing it (throws exception if empty)


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.