Introduction To Queue In Java

A queue is an object that represents a data structure designed to have the element inserted at the end of the queue, and the element removed from the beginning of the queue.

Java.Util.Queue contains multiple elements before the process. The order of elements of the queue in Java is FIFO (first-in-first-out). It provides additional operations such as insertion, inspection, and deletion.

Implementation of Queue in Java

The queue is an interface that is required to manifest a concrete implementation of the interface to use. Implementation of Java is below:

Java.util.LinkedList

Java.util.PriorityQueue

Linked List:

A linked list is a data structure similar to arrays. It interconnects each node to the next node through a memory address link. A linked list has three elements:

  • Head
  • Nodes
  • Tail

Priority Queue:

A priority queue is a collection of items in which all are of the same types. It enables the perfect insertion of elements and efficient removal of the minimum element. It bases removal on the natural order or by the comparator. The priority queue is also an abstract data type (ADT).

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

Generic Queue in Java

This class declaration looks similar to a non-generic class declaration, but a type parameter section follows the class name. As a generic method, the section of parameter type of a generic class can have one or more types of parameters: it can separate commas. These classes are known as parameterized type or class because they accept one or more parameters.

Classes That Implement the Queue Interface

Java Queue interface enlarges the collection of interfaces. In the collection of an interface, the iterable interface gets extended. The classes of implementations of a queue are as follows: 

LinkedList, PriotityQueue, ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriotityBlockingQueue, etc. The purpose of the queue interface is to reduce the effort to implement the queue.

Add an Element to a Queue

The Queue interface includes two methods that can be used to add elements to a queue.

Add ():

  Add is used to insert a specified element into the queue. It returns true when the task is successful or else it throws an exception.

Offer ()

   Offer is used to insert a specified element into the queue. It returns true when the task is successful or else its return false.

Example 1:

 Code:

// adding elements into Queue

public static void main(String[] arg) {

// declaring variable 

// Queue is a interface it has two methods to add elements

Queue<Integer> queueOne = new LinkedList<>();

queueOne.add(6); // add method to use insert element

queueOne.add(1);

queueOne.add(8);

queueOne.add(4);

queueOne.add(7);

      System.out.println("Example with Add method The queue is: " + queueOne);

    Queue<String> queueTwo = new LinkedList<>();

    queueTwo.offer("One"); // offer method to use insert element

    queueTwo.offer("Two");

    queueTwo.offer("Three");

    queueTwo.offer("Four");

    queueTwo.offer("Five");

      System.out.println("Example with Offer method The queue is: " + queueTwo);

}

Output:

Example with Add method The queue is: [6, 1, 8, 4, 7]

Example with Offer method The queue is: [One, Two, Three, Four, Five]

Screenshot

QueueInJavaEx1_1.

QueueInJavaEx1_2

Element to a queue in Java

The elements of Java queue using the methods poll () either Remove ()

Remove ():

Remove is used to return the head of the queue when the task is successful or else throws an exception if the queue is empty.

Example 2:

Code:

// remove elements from Queue

public static void main(String[] arg) {

// declaring variable 

// Queue is a interface it has two methods to add elements

Queue<Integer> queueOne = new LinkedList<>();

queueOne.add(6); // add method to use insert element

queueOne.add(1);

queueOne.add(8);

queueOne.add(4);

queueOne.add(7);

System.out.println("Before remove and poll method The queue is: " + queueOne);

// poll method to remove top of the element in Queue

int positionOne = queueOne.poll();

System.out.println("Removed Element value from Queue : "+positionOne);

// remove method to remove top of the element in Queue

int positionTwo = queueOne.remove();

System.out.println("Removed Element value from Queue : "+positionTwo);

    System.out.println("After remove and poll method The queue is: " + queueOne);  

}

Output:

Before remove and poll method, the queue is: [6, 1, 8, 4, 7]

Removed Element value from Queue: 6

Removed Element value from Queue: 1

After remove and poll method, the queue is: [8, 4, 7]

Screenshot:

QueueInJavaEx2_1

QueueInJavaEx2_2

Poll ():

Remove is used to return the head of the queue when the task is successful or else it returns null if the queue is empty.

Peek at the Queue in Java

The queue without taking the element out of the queue of the methods is element () or peek ()

Element ();

Element is used to return the head of the queue when the task is successful or else throws an exception if the queue is empty.

Peek ():

Peek is used to return the head of the queue when the task is successful or else it returns null if the queue is empty.

Example 3

Code:

// peek the elements from Queue

public static void main(String[] arg) {

// declaring variable 

// Queue is a interface it has two methods to add elements

Queue<Integer> queueOne = new LinkedList<>();

queueOne.add(6); // add method to use insert element

queueOne.add(1);

queueOne.add(8);

queueOne.add(4);

queueOne.add(7);

System.out.println("The queue is: " + queueOne);

// peek method returns the first element from the Queue

int positionpeek = queueOne.peek();

// Element method returns the first element from the Queue

int positionElement = queueOne.element();

System.out.println("using Peek method first value from Queue : "+positionpeek);

System.out.println("using Element method first value from Queue : "+positionElement);

}

Output:

The queue is: [6, 1, 8, 4, 7]

using Peek method first value from Queue: 6

using Element method first value from Queue: 6

Screenshots

QueueInJavaEx3_1.

QueueInJavaEx3_2.

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Remove the Elements From Queue in Java

It is used to return and remove the head of the queue when the task is successful, or else it throws an exception if the queue is empty. It has inherited the collection interface.

Iterate Elements in Queue in JAVA

In this queue, there are multiple ways to iterate through the queue including the way of converting the queue to the array and traverse the for loop. It also has an inbuilt iterator that can be used to iterate through the queue. 

Example 4

 Code:

// Iterate elements in Queue

public static void main(String[] arg) {

// declaring variable 

// Queue is a interface it has two methods to add elements

Queue<Integer> queueOne = new LinkedList<>();

queueOne.add(6); // add method to use insert element

queueOne.add(1);

queueOne.add(8);

queueOne.add(4);

queueOne.add(7);

System.out.println("The queue is: " + queueOne);

//access via Iterator

Iterator<Integer> iterator = queueOne.iterator();

while(iterator.hasNext()){

  Integer iteratedvalue = iterator.next(); // next is a inbuilt method in Iterator class

  // to use iterate the queue one by one

  System.out.println("Iterated value is : " + iteratedvalue);

}

}

Output

The queue is: [6, 1, 8, 4, 7]

Iterated value is: 6

Iterated value is: 1

Iterated value is: 8

Iterated value is: 4

Iterated value is: 7

ScreenShot

QueueInJavaEx4_1

QueueInJavaEx4_2 

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

Queue in Java is a linear data structure where you can handle an ordered collection of elements. It follows the FIFO principle to add elements from one end and remove them from the other end. You have seen the methods add, offer, poll, and remove. It is recommended to apply Queue in situations where data doesn’t need synchronous transfers. LinkedList class implements the unbounded (not limited by size) Queue Interface. Along with Queue in Java, you can also try Priority Queue, Double-Ended Queue, and Circular Queue.

Simplilearn's java certification program will help you expand your horizons by growing your knowledge of Java Data Forms. The course includes 60 hours of applied learning and 35 certified coding-related activities that will provide you with hands-on experience. This Full Stack Java Developer course will teach you the fundamentals of front-end, middleware, and back-end Java web development. You'll learn how to create an end-to-end application, test and deploy code, and use MongoDB to store data, among other things.

Do you need any more information or have any questions for us on this “queue in Java” article? iF you, don’t hesitate to place them as comments towards the end of the page. We will get to them and respond soon.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.