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

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago