SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Java Queue Interface

  • If you look at collection hierarchy, Queue extends collection interface.
  • Main purpose of collection is to hold the elements prior to processing.
  • Queue interface provides some additional operations like insertion, inspection and extraction besides collection interface operations.
  • All these three operations exist in 2 different forms. One return special value(null or false depending on the operation) if operation fails else throws an exception if operation fails.
  • Queue follows FIFO(first-in-first-out) so it will insert element at the end of the queue when you insert new element and element will be removed from the beginning of the queue when you remove element.
  • As Queue interface is sub type of collection interface, all methods of collection interface are also available in Queue interface.
Queue Interface hierarchy


 Basic methods of Queue interface
Few of the basic Queue interface methods are as below.
  • boolean add(E e) : It will insert an element in queue if space is available in queue. Else it will return IllegalStateException.
  • E element() : It will return head(first element) of queue if queue in not empty. It will return NoSuchElementException exception if Queue is empty.
  • boolean offer(E e) : It will insert element in queue if space is available in queue.
  • E peek() : It will return head(first element) of queue if queue in not empty. Else it will return null.
  • E poll() : It will retrieve and remove head(first element) of the element. Return null if queue is empty.
  • E remove() : It will retrieve and remove head(first element) of the element. It will return NoSuchElementException exception if Queue is empty.
I have prepared example on basic Queue interface methods to show demo how they works.

Queue Interface Example
package JAVAExamples;

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {

 public static void main(String[] args) {
  //Creating queue.
  Queue q = new LinkedList();
  
  //Inserting elements in queue.
  q.add("three");
  q.add("two");
  q.add("one");  
  q.add("four");
  q.add("six");
  q.add("seven");
  q.add("five");
  
  //Print queue elements.
  System.out.println("Queue elements are : "+q);
  
  //Removing first inserted element from queue using remove method.
  q.remove();
  System.out.println("Queue elements after element removal using remove method : "+q);
  
  //retrieve head of queue (first element) using element method.
  System.out.println("Now first element in queue is : "+q.element());
  
  //Insert element in queue using offer method. 
  q.offer("eight");
  System.out.println("Queue elements after inserting element using offer method : "+q);
  
  //Removing specific element from queue.
  q.remove("six");
  System.out.println("Queue elements after removing six element : "+q);

  //Removing head of queue (first element) using poll method.
  q.poll();
  System.out.println("Queue elements after removing element using poll method : "+q);
  
  //retrieve head of queue (first element) using peek method.
  System.out.println("Now first element in queue is : "+q.peek());  
 }
}

Output :
Queue elements are : [three, two, one, four, six, seven, five]
Queue elements after element removal using remove method : [two, one, four, six, seven, five]
Now first element in queue is : two
Queue elements after inserting element using offer method : [two, one, four, six, seven, five, eight]
Queue elements after removing six element : [two, one, four, seven, five, eight]
Queue elements after removing element using poll method : [one, four, seven, five, eight]
Now first element in queue is : one

No comments:

Post a Comment