Categories: Java

Queue Interface in Java

The Queue interface extends the Collection interface and is present in java.util package is used to hold the elements about to be processed in FIFO (First In First Out) order. It is an ordered list of objects that can only be used to insert elements at the end of the list and delete elements from the beginning of the list (i.e., it adheres to the FIFO or First-In-First-Out principle). Queues provide additional insertion, removal, and inspection operations in addition to basic Collection operations.

Declaration:
public interface Queue extends Collection
Method of Queue interface:
Method Description
boolean add(E e)Inserts the specified element into this queue as soon as possible without violating capacity constraints, returning true on success and throwing an IllegalStateException if no space is currently available.
E element()The head of this queue is retrieved but not removed.
boolean offer(E e)Inserts the specified element into this queue as soon as possible without violating capacity constraints.
E peek()Retrieves the head of this queue but does not remove it, or returns null if the queue is empty.
E poll()This method retrieves and removes the queue’s head, or returns null if the queue is empty.
E remove()This function retrieves and removes the queue’s head.
Example:

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

public class Coderz {

 public static void main(String[] args)
 {
     Scanner sc= new Scanner(System.in);
  Queue<Integer> q
   = new LinkedList<>();

 
  for (int i = 0; i < 5; i++)
   q.add(sc.nextInt());

  // Display contents of the queue.
  System.out.println("Elements of queue "
      + q);

  // To remove the head of queue.
  int removedele = q.remove();
  System.out.println("removed element "
      + removedele);

  System.out.println(q);

  // To view the head of queue
  int head = q.peek();
  System.out.println("head of queue "
      + head);

  // Rest all methods of collection
  // interface like size and contains
  // can be used with this
  // implementation.
  int size = q.size();
  System.out.println("Size of queue "
      + size);
 }
}
Output:
2
3
4
5
6
Elements of queue [2, 3, 4, 5, 6]
removed element 2
[3, 4, 5, 6]
head of queue 3
Size of queue 4

Note: also read about the SortedMap Interface in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago