boolean
add(E e)
Inserts
the specified element into the queue. return true upon success and
throwing an IllegalStateException if no space is currently available
(capacity-restricted deque). This method is equivalent to addLast.
import java.util.*; import java.util.concurrent.LinkedBlockingDeque; class DequeAdd{ public static void main(String args[]){ Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (4); System.out.println("Is 10 added to the Deque " + myDeque.add(10)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 20 added to the Deque " + myDeque.add(20)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 30 added to the Deque " + myDeque.add(30)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 40 added to the Deque " + myDeque.add(40)); System.out.println("Elements in Deque are " + myDeque); } }
Output
Is 10 added to the Deque true Elements in Deque are [10] Is 20 added to the Deque true Elements in Deque are [10, 20] Is 30 added to the Deque true Elements in Deque are [10, 20, 30] Is 40 added to the Deque true Elements in Deque are [10, 20, 30, 40]
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 DequeAddIllegal{ public static void main(String args[]){ Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (4); System.out.println("Is 10 added to the Deque " + myDeque.add(10)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 20 added to the Deque " + myDeque.add(20)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 30 added to the Deque " + myDeque.add(30)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 40 added to the Deque " + myDeque.add(40)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 50 added to the Deque " + myDeque.add(50)); System.out.println("Elements in Deque are " + myDeque); } }
Output
Is 10 added to the Deque true Elements in Deque are [10] Is 20 added to the Deque true Elements in Deque are [10, 20] Is 30 added to the Deque true Elements in Deque are [10, 20, 30] Is 40 added to the Deque true Elements in Deque are [10, 20, 30, 40] Exception in thread "main" java.lang.IllegalStateException: Deque full at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:332) at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:631) at DequeAddIllegal.main(DequeAddIllegal.java:20)
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 DequeAddNull{ public static void main(String args[]){ Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> (); System.out.println("\nAdd null to the Deque"); myDeque.add(null); System.out.println("Elements in Deque are\n" + myDeque); } }
Output
Add null to the Deque Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ConcurrentLinkedDeque.checkNotNull(ConcurrentLinkedDeque.java:800) at java.util.concurrent.ConcurrentLinkedDeque.linkLast(ConcurrentLinkedDeque.java:388) at java.util.concurrent.ConcurrentLinkedDeque.offerLast(ConcurrentLinkedDeque.java:930) at java.util.concurrent.ConcurrentLinkedDeque.add(ConcurrentLinkedDeque.java:1024) at DequeAddNull.main(DequeAddNull.java:9)
4.
throws IllegalArgumentException if some property of the specified
element
prevents it from being added to this deque
No comments:
Post a Comment