remove( ) method of Queue Interface in Java.

remove( ) method of Queue Interface in Java.


Posted in : Core Java Posted on : January 31, 2011 at 6:16 PM Comments : [ 0 ]

In this section we will discuss how can remove( ) method be implemented in Queue interface in java.

remove( ) method of Queue Interface in Java.

In this section we will discuss how can remove( ) method be implemented in Queue interface in java.

Syntax

E remove()

This method removes the element from the top of the queue.

In this method you can retrieve and delete the element at the top of the queue. Precisely an element is removed from the queue is an operation of the queue's arranging policy which may be different from its implementation. This method displays 'NoSuchElementException' exception if the queue is empty.

Parameter description 

This method does not take any argument but, it returns the head of the queue.

Example of remove( ) method 

In this example we will show you, how does remove( ) method work in Queue interface. This Example will help you to understand  how an element can be removed from the head of the queue. Through this example we will show how can you delete the head of the underlying Queue, and count the total number of elements in queue before and after removing the elements.

 Example :

package devmanuals.com;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Iterator;
public class QueueRemove {
  public static void main(String[] args) {
    Queue<Integer> q = new LinkedList<Integer>();    
    q.add(1);
    q.add(2);
    q.add(3);
    q.add(4);
    q.add(5);
    System.out.println("Is queue empty : "+q.isEmpty());
    System.out.println("Elements of queue : "+q);
    System.out.println("Size of queue before removing element : "+q.size());
    System.out.println("Now after removing the head of the queue new queue is");
    Object obj=q.remove();
    System.out.println(q);
    System.out.println("Removed element = "+obj);
    System.out.println("Size of queue after removing element : "+q.size());
  System.out.println("Removes all the elements from queue");
    Iterator lq = q.iterator();
  while(lq.hasNext())
    q.remove();
  System.out.println(q);
  System.out.println("Is queue empty : "+q.isEmpty());
  //Here implementation of the remove( ) method will throw an exception 
  q.remove();
  }
}

Output :

Is queue empty : false

Elements of queue : [1, 2, 3, 4, 5]

Size of queue before removing element : 5

Now after removing the head of the queue new queue is

[2, 3, 4, 5]

Removed element = 1

Size of queue after removing element : 4

Removes all the elements from queue

[]

Is queue empty : true

Exception in thread "main" java.util.NoSuchElementException

at java.util.LinkedList.remove(Unknown Source)

at java.util.LinkedList.removeFirst(Unknown Source)

at java.util.LinkedList.remove(Unknown Source)

at devmanuals.com.QueueRemove.main(QueueRemove.java:31) 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics