Saturday 12 April 2014

remove : Removes the first occurrence of the element

boolean remove(Object o)
Removes the first occurrence of the specified element from this deque. Returns true if this deque changed as a result of the call.

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequeRemove{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  myDeque.add(10);
  myDeque.add(20);
  myDeque.add(30);
  myDeque.add(40);
  myDeque.add(10);
  System.out.println("Elements in the Deque are "+ myDeque);

  System.out.println("\nIs 10 removed " + myDeque.remove(10));
  System.out.println("Elements in the Deque are "+ myDeque);

  System.out.println("\nIs 90 removed " + myDeque.remove(90));
  System.out.println("Elements in the Deque are "+ myDeque);
 }
}

Output
Elements in the Deque are [10, 20, 30, 40, 10]

Is 10 removed true
Elements in the Deque are [20, 30, 40, 10]

Is 90 removed false
Elements in the Deque are [20, 30, 40, 10]

 
1. throws ClassCastException if the class of the specified element is incompatible with this deque

2. throws NullPointerException if the specified element is null and this
deque does not permit null elements
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;

class DequeRemoveEleNull{
  public static void main(String args[]){
   Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> ();
  
   System.out.println("\nRemove null from the Deque");
   myDeque.remove(null);
   System.out.println("Elements in Deque are\n" + myDeque);
  }
}

Output
Remove null from the Deque
Exception in thread "main" java.lang.NullPointerException
        at java.util.concurrent.ConcurrentLinkedDeque.checkNotNull(ConcurrentLin
kedDeque.java:800)
        at java.util.concurrent.ConcurrentLinkedDeque.removeFirstOccurrence(Conc
urrentLinkedDeque.java:1044)
        at java.util.concurrent.ConcurrentLinkedDeque.remove(ConcurrentLinkedDeq
ue.java:1138)
        at DequeRemoveEleNull.main(DequeRemoveEleNull.java:9)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment