Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday, 1 July 2017

Java Queue


Java Queue Interface - Collection Framework

Java queue is an interface in java collection framework and queue interface extends collection interface. The queue interface is available in java.util.* package.

Java queue interface defines queue data structure which is normally First-In-First-Out(FIFO). In other words, Java Queue interface maintains the order of an elements in FIFO(First-In-First-Out) manner.

Java Queue interface is a data structure in which elements are added from one end and elements are deleted from another end.

In java queue, first element removed first and last element removed last.

The queue interface can be dynamically changed  during the processing i.e processed elements are removed from the queue and new elements are added to the queue.

Java collection framework contains Queue interface and also there are some other sub-interfaces which are Deque, BlockingDeque, BlockingQueue and TransferQueue.


Hierarchy of Java Queue Interface

This is the hierarchy of Queue interface in java.
Java Queue

In the given above diagram, Queue interface extends Collection interface . The PriorityQueue class extends Queue interface and Deque interface extends Queue interface.


Methods of Java Queue Interface

There are some methods of queue interface in java and these are given below :

1) boolean add(Object o)

This method inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

2) boolean offer(Object o)

This method inserts the specified elements into this queue if it is possible to do so immediately without violating capacity restrictions.

3) Object peek()

This method retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

4) Object remove()

This method retrieves and removes the head of this queue.

5) Object poll()

This method retrieves and removes the head of this queue, or returns null if this queue is empty.

6) Object element()

This method retrieves but doesn't removes the head of this queue.


Java PriorityQueue Class

Java PriorityQueue is a class which extends AbstractQueue class and implements Queue and Serializable interface in java.

PriorityQueue class is available in java.util.* package.

PriorityQueue class doesn't maintains the order of an elements in FIFO(First-In-First-Out) manner. This class provides the facility of using the queue in java.


Java PriorityQueue Class Example

This is an example of priority queue class where we will use some methods of Queue interface because priority queue class inherit queue interface and we will traverse the elements and removes some elements from the queue.

import java.util.*;
public class PriorityQueueExample
{
public static void main(String args[])
{
//declaring the PriorityQueue class
PriorityQueue<String> queue = new PriorityQueue<String>();
queue.add("salman");
queue.add("ranveer");
queue.add("boman");
queue.add("virat");
queue.add("dhoni");
queue.add("rohit");
System.out.println("head "+queue.element());
System.out.println("head "+queue.peek());
System.out.println("Traverse the queue elements");
Iterator itr = queue.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("after removing two elements :");
Iterator<String> itr2 = queue.iterator();
while(itr2.hasNext())
{
System.out.println(itr2.next());
}
}
}

output : head boman
              head boman
              Traverse the queue elements
              boman
              dhoni
              ranveer
              virat
              salman
              rohit
              after removing two elements :
              ranveer 
              rohit
              salman 
              virat

Share:

3 comments:

  1. do you have topic of deque, i need it.... tks

    ReplyDelete
  2. nice java blog and very helpfull.
    thanks.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate