BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Queue Interface in Java Collections

Last Updated: November 8, 2022 by Chaitanya Singh | Filed Under: java

A Queue is designed in such a way so that the elements added to it are placed at the end of Queue and removed from the beginning of Queue. The concept here is similar to the queue we see in our daily life, for example, when a new iPhone launches we stand in a queue outside the apple store, whoever is added to the queue has to stand at the end of it and persons are served on the basis of FIFO (First In First Out), The one who gets the iPhone is removed from the beginning of the queue.

Queue Interface Java Hierarchy


Queue interface in Java collections has two implementation: LinkedList and PriorityQueue, these two classes implements Queue interface.
Queue is an interface so we cannot instantiate it, rather we create instance of LinkedList or PriorityQueue and assign it to the Queue like this:

Queue q1 = new LinkedList();
Queue q2 = new PriorityQueue();

Java Queue Example

import java.util.*;
public class QueueExample1 {
	 
   public static void main(String[] args) {
	  
      /*
       * We cannot create instance of a Queue as it is an
       * interface, we can create instance of LinkedList or
       * PriorityQueue and assign it to Queue
       */
      Queue<String> q = new LinkedList<String>();
	    
      //Adding elements to the Queue
      q.add("Rick");
      q.add("Maggie"); 
      q.add("Glenn");
      q.add("Negan");
      q.add("Daryl");
	    
      System.out.println("Elements in Queue:"+q);

      /*
       * We can remove element from Queue using remove() method,
       * this would remove the first element from the Queue 
       */
      System.out.println("Removed element: "+q.remove());
	    
      /*
       * element() method - this returns the head of the
       * Queue. Head is the first element of Queue
       */
      System.out.println("Head: "+q.element());
	    
      /*
       * poll() method - this removes and returns the 
       * head of the Queue. Returns null if the Queue is empty
       */
      System.out.println("poll(): "+q.poll());
	    
      /*
       * peek() method - it works same as element() method,
       * however it returns null if the Queue is empty
       */
      System.out.println("peek(): "+q.peek());
	    
      //Again displaying the elements of Queue
      System.out.println("Elements in Queue:"+q);
   }
}

Output:

Elements in Queue:[Rick, Maggie, Glenn, Negan, Daryl]
Removed element: Rick
Head: Maggie
poll(): Maggie
peek(): Glenn
Elements in Queue:[Glenn, Negan, Daryl]

In the above example, I have used Generics, this helps us to specify the type of the element that we are going to insert into the collection. As you can see I have specified the type of the Queue as String using Generics, so that it accepts only String elements. If you try to add the element of the type not specified then you will get compilation error, this brings the safety in our program and makes it less error prone.

Methods of Queue interface

boolean add(E e): This method adds the specified element at the end of Queue. Returns true if the the element is added successfully or false if the element is not added that basically happens when the Queue is at its max capacity and cannot take any more elements.

E element(): This method returns the head (the first element) of the Queue.

boolean offer(object): This is same as add() method.

E remove(): This method removes the head(first element) of the Queue and returns its value.

E poll(): This method is almost same as remove() method. The only difference between poll() and remove() is that poll() method returns null if the Queue is empty.

E peek(): This method is almost same as element() method. The only difference between peek() and element() is that peek() method returns null if the Queue is empty.

We have seen how a queue serves the requests on the basis of FIFO(First in First out). What if we want to serve the request based on the priority rather than FIFO? This can be done with the help of PriorityQueue, which serves the request based on the priority, that we set using Comparator.

Recommended Posts

  • Java LinkedList peek(), peekFirst() and peekLast() Methods
  • Java LinkedList poll(), pollFirst() and pollLast() Methods

Top Related Articles:

  1. Comparator Interface in Java
  2. PriorityQueue Interface in Java Collections
  3. LinkedList push() and pop() methods – Java
  4. Difference between ArrayList and Vector in Java
  5. Java – Remove specific elements from LinkedList example

Tags: Collections

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap