AppDividend
Latest Code Tutorials

Java Queue Interface Tutorial With Example | Queue Interface in Java

0

Java Queue Interface Tutorial With Example | Queue Interface in Java is today’s topic. The Queue is a data structure we can insert an element at that end of a queue and remove from at the beginning of the queue. It follows the FIFO (First In First Out) principle.

We go to ATM for withdrawing cash, and there we use this Queue concept (if there are a crowd). We have to maintain a line which is nothing but a Queue. Who is standing at the first of the queue, gets the first service and who stand at the last of the queue, gets the last service.

 

Java Queue Interface Tutorial With Example

Java Queue Interface Tutorial With Example

The Queue interface is available in java.util package and extends a Collection interface. Queue collection is used to hold the items about to be processed and provides various operations like an insertion, removal, etc.

Queue is an ordered list of objects with its use limited to insert elements at the end of the list and remove items from the start of the list.

The queue is an interface, so we need concrete classes for the declaration; in this case, most common concrete classes are LinkedList and PriorityQueue. We create an instance of LinkedList and PriorityQueue and assign it to the queue like the following syntax.

Queue q = new LinkedList ();
Queue q = new PriorityQueue ();

Characteristics of Queue

The Queue is used to add elements at the end of the queue and deletes from the beginning of the queue. It follows the FIFO concept(First In First Out).

  1. The Java Queue supports all the methods of Collection interface, including an insertion, deletion, etc.
  2. The LinkedList, ArrayBlockingQueue, and PriorityQueue are the most frequently used implementations.
  3. If any null operation is performed on the BlockingQueues, NullPointerException is thrown.
  4. BlockingQueues have a thread-safe implementations.
  5. The Queues which are available in java.util package is Unbounded Queues
  6. The Queues which are available in the java.util.concurrent package is a Bounded Queues.
  7. All Queues except the Deques supports the insertion and removal at the tail and head of the queue respectively. The Deque supports item insertion and removal at both ends.

Methods of the Queue Interface

There are various methods of queue interface in java by which we can implement the queue data structure.

boolean add()

It is used to insert any specific element at the end of a queue. As its return type is boolean, it returns true if the element is successfully added to the queue or returns false if not added to the queue. It happens when the queue crosses its maximum capacity.

// Add.java

import java.util.LinkedList;
import java.util.Queue;
class Add
{
	public static void main(String []er)
	{
		//creating instance of LinkedList to decalre methods of Queue
		Queue<String>q=new LinkedList<String>();
		
		//adding elements into queue 
		
		q.add("AI");
		q.add("Java");
		//also used to add element
		q.offer("Machine Learning");
		q.add("App Development");
		System.out.println("Elements in Queues are:"+q);
		
	}
}

See the below output.

boolean add()

 

boolean offer()

// Add.java

import java.util.LinkedList;
import java.util.Queue;
class Add
{
	public static void main(String []er)
	{
		//creating instance of LinkedList to decalre methods of Queue
		Queue<String>q=new LinkedList<String>();
		
		//adding elements into queue 
		
		q.add("AI");
		q.add("Java");
		//also used to add element
		q.offer("Machine Learning");
		q.add("App Development");
		System.out.println("Elements in Queues are:"+q);
		
	}
}

It also adds the specified element to queue. It performs the same work as add() method. The output is the same as the previous program.

boolean add()

Object remove()

It is used to retrieve or remove the head (the first element) of the queue and returns its value.

// Remove.java

//importing queue and linkedlist 
import java.util.LinkedList;
import java.util.Queue;
class Remove
{
	public static void main(String []er)
	{
		//creating instance of LinkedList to decalre methods of Queue
		Queue<String>q=new LinkedList<String>();
		
		//adding elements into queue 
		
		q.add("AI");
		q.add("Java");
		//also used to add element
		q.offer("Machine Learning");
		q.add("App Development");
		System.out.println("Elements in Queues are before using remove method:"+q);
		//remove the head element of the queue 
		
		q.remove();
		
		System.out.println("Elements in Queues are after using remove method:"+q);
		
		//removing element using poll method
		q.poll();
		
		System.out.println("Elements in Queues are after using poll method:"+q);
	}
}

See the below output.

Object remove() example

 

 

Object peek()

The peek() method returns the head of the queue. See the following program.

// Peek.java

//importing queue and linkedlist 
import java.util.LinkedList;
import java.util.Queue;
class Peek
{
	public static void main(String []er)
	{
		//creating instance of LinkedList to decalre methods of Queue
		Queue<String>q=new LinkedList<String>();
		
		//adding elements into queue 
		
		q.add("AI");
		q.add("Java");
		//also used to add element
		q.offer("Machine Learning");
		q.add("App Development");
		System.out.println("Elements in Queues are:"+q);
		//using peek method
		System.out.println("The head element of queue is (using peek () method) :"+q.peek());
		
		//using element() method
		System.out.println("The head element of queue is (using element() method):"+q.element());
		
	}
}

See the following output.

Object Peek() example

 

 

Object element()

An element() method also returns the head of the queue. But difference b/w element () method and peek() returns null if the queue is empty.

See the following program.

// Peek.java

//importing queue and linkedlist 
import java.util.LinkedList;
import java.util.Queue;
class Peek
{
	public static void main(String []er)
	{
		//creating instance of LinkedList to decalre methods of Queue
		Queue<String>q=new LinkedList<String>();
		
		//adding elements into queue 
		
		q.add("AI");
		q.add("Java");
		//also used to add element
		q.offer("Machine Learning");
		q.add("App Development");
		System.out.println("Elements in Queues are:"+q);
		//using peek method
		System.out.println("The head element of queue is (using peek () method) :"+q.peek());
		
		//using element() method
		System.out.println("The head element of queue is (using element() method):"+q.element());
		
	}
}

See the following output.

Object Peek() example

 

Object poll()

The poll() method is similar to remove () method, but the only difference is poll () returns null if the queue is empty.

// Remove.java

//importing queue and linkedlist 
import java.util.LinkedList;
import java.util.Queue;
class Remove
{
	public static void main(String []er)
	{
		//creating instance of LinkedList to decalre methods of Queue
		Queue<String>q=new LinkedList<String>();
		
		//adding elements into queue 
		
		q.add("AI");
		q.add("Java");
		//also used to add element
		q.offer("Machine Learning");
		q.add("App Development");
		System.out.println("Elements in Queues are before using remove method:"+q);
		//remove the head element of the queue 
		
		q.remove();
		
		System.out.println("Elements in Queues are after using remove method:"+q);
		
		//removing element using poll method
		q.poll();
		
		System.out.println("Elements in Queues are after using poll method:"+q);
	}
}

 

Object poll() example

size()

The size() method return the no. of elements in the queue. Since it is a subtype of Collections class, it inherits all the methods of it namely size(), isEmpty(), contains() etc.

Finally, Java Queue Interface Tutorial With Example | Queue Interface in Java article is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.