Saturday 12 April 2014

push : Pushes an element onto the deque

void push(E e)
Push an element at the head of this deque. Return true upon success and throwing an IllegalStateException if no space is currently available.

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

class DequePush{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  myDeque.push(10);
  System.out.println("10 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(20);
  System.out.println("\n20 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(30);
  System.out.println("\n30 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(40);
  System.out.println("\n40 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(50);
  System.out.println("\n50 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
 }
}
 
Output
10 pushed to Deque
Elements in the Deque are [10]

20 pushed to Deque
Elements in the Deque are [20, 10]

30 pushed to Deque
Elements in the Deque are [30, 20, 10]

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

50 pushed to Deque
Elements in the Deque are [50, 40, 30, 20, 10]

1. throws IllegalStateException if the element cannot be added at this
time due to capacity restrictions
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequePushIllegal{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  myDeque.push(10);
  System.out.println("10 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(20);
  System.out.println("\n20 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(30);
  System.out.println("\n30 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(40);
  System.out.println("\n40 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(50);
  System.out.println("\n50 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
  
  myDeque.push(60);
  System.out.println("\n60 pushed to Deque ");
  System.out.println("Elements in the Deque are "+ myDeque);
 }
}

Output
10 pushed to Deque
Elements in the Deque are [10]

20 pushed to Deque
Elements in the Deque are [20, 10]

30 pushed to Deque
Elements in the Deque are [30, 20, 10]

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

50 pushed to Deque
Elements in the Deque are [50, 40, 30, 20, 10]
Exception in thread "main" java.lang.IllegalStateException: Deque full
 at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:323)
        at java.util.concurrent.LinkedBlockingDeque.push(LinkedBlockingDeque.jav
a:766)
        at DequePushIllegal.main(DequePushIllegal.java:28)

2. throws ClassCastException if the class of the specified element prevents it from being added to this deque

3. 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 DequePushNull{
  public static void main(String args[]){
   Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> ();
  
   System.out.println("\nPush null to the Deque");
   myDeque.push(null);
   System.out.println("Elements in Deque are\n" + myDeque);
  }
}

Output
Push null to the Deque
Exception in thread "main" java.lang.NullPointerException
        at java.util.concurrent.ConcurrentLinkedDeque.checkNotNull(ConcurrentLin
kedDeque.java:800)
        at java.util.concurrent.ConcurrentLinkedDeque.linkFirst(ConcurrentLinked
Deque.java:355)
        at java.util.concurrent.ConcurrentLinkedDeque.addFirst(ConcurrentLinkedD
eque.java:892)
        at java.util.concurrent.ConcurrentLinkedDeque.push(ConcurrentLinkedDeque
.java:1031)
        at DequePushNull.main(DequePushNull.java:9)

4. throws IllegalArgumentException if some property of the specified
element prevents it from being added to this deque.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment