«Back to Home

Core Java

Topics

Queue Interface In Java

Queue Interface
 
In Java, the queue interface basically orders the element in FIFO (First In First Out) manner.
 
Methods of Queue Interface
  • public boolean add(object);

  • public boolean offer(object);

  • public remove();

  • public poll();

  • public element();

  • public peek();
PriorityQueue class

In Java, PriorityQueue class provides all the facility of using queue. It does not order the elements in FIFO manner.
 
PriorityQueue class is an unbounded priority queue, based on a priority heap. The priority queue elements are ordered, according to their usual ordering or by a comparator, provided at a queue construction time, depending on which constructor are used. It does not allow null elements and relying on natural ordering also does not allow insertion of non-comparable objects.
 
Constructor of PriorityQueue Class

E is the type of elements, held in the collection.

PriorityQueue()

This constructor creates a PriorityQueue with the default initial capacity, which orders its elements , according to their natural ordering.
 
PriorityQueue(Collection<? extends E> c)

This constructor creates a PriorityQueue, which contains the elements in the specified collection.
 
PriorityQueue(int initialCapacity)

This constructor creates a PriorityQueue with the specified initial capacity, which orders its elements , according to their natural ordering.
 
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)

This constructor creates a PriorityQueue with the specified initial capacity, which orders its elements , according to the specified comparator.
 
PriorityQueue(PriorityQueue<? extends E> c)

This constructor creates a PriorityQueue, which contains the elements in the specified priority queue.
 
PriorityQueue(SortedSet<? extends E> c)

This constructor creates a PriorityQueue, which contains the elements in the specified sorted set.
 
Methods of PriorityQueue Class

boolean add(E e)

This method is used to insert the specified element into the priority queue.
 
void clear()

This method is used to remove all of the elements from the priority queue.
 
Comparator<? super E> comparator()

This method is used to return the comparator used to order the elements in the queue or null, if the queue is sorted, according to the natural ordering of its elements.
 
boolean contains(Object o)

This method is used to return true, if this queue contains the specified element.
 
Iterator<E> iterator()

This method is used to return an iterator over the elements in the queue.
 
boolean offer(E e)

This method is used to insert the specified element into the priority queue.
 
E peek()

This method is used to retrieve but does not remove the head of this queue or returns null, if the queue is empty.
 
E poll()

This method is used to retrieve and remove the head of the queue or returns null, if the queue is empty.
 
boolean remove(Object o)

This method is used to remove a single instance of the specified element from the queue, if it is present.
 
int size()

This method is used to return the number of elements in the collection.
 
Object[] toArray()

This method is used to return an array, which contains all the elements in the queue.
 
<T> T[] toArray(T[] a)

This method is used to return an array, which contains all the elements in the queue; the runtime type of the returned array is that of the specified array.
 
Let’s see an example, given below.
 
Code
  1. import java.util.*;  
  2. public class CollectionsExample {  
  3.     public static void main(String args[]) {  
  4.         PriorityQueue < String > q = new PriorityQueue < String > ();  
  5.         q.add("Harry");  
  6.         q.add("James");  
  7.         q.add("David");  
  8.         System.out.println(q.element());  
  9.         System.out.println(q.peek());  
  10.         System.out.println("Queue elements:");  
  11.         Iterator i = q.iterator();  
  12.         while (i.hasNext()) {  
  13.             System.out.println(i.next());  
  14.         }  
  15.         q.remove();  
  16.         q.poll();  
  17.         System.out.println("After removing elements:");  
  18.         Iterator < String > it = q.iterator();  
  19.         while (it.hasNext()) {  
  20.             System.out.println(it.next());  
  21.         }  
  22.     }  
  23. }  
38

Output

39
 
Summary

Thus, we learnt that Java queue interface basically orders the element in FIFO (First In First Out) manner and we also learnt how we can use PriorityQueue class in Java.