boolean
offer(E e)
Inserts
the specified element into the queue represented by this deque.
returning true
upon success and false if no space is currently available. This
method is equivalent to offerLast.
import java.util.*; import java.util.concurrent.LinkedBlockingDeque; class DequeOffer{ public static void main(String args[]){ Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (4); System.out.println("Is 10 added to the Deque " + myDeque.offer(10)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 20 added to the Deque " + myDeque.offer(20)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 30 added to the Deque " + myDeque.offer(30)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 40 added to the Deque " + myDeque.offer(40)); System.out.println("Elements in Deque are " + myDeque); System.out.println("\nIs 50 added to the Deque " + myDeque.offer(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] Is 50 added to the Deque false Elements in Deque are [10, 20, 30, 40]
1.
throws ClassCastException if the class of the specified element
prevents it from being added to 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 DequeOfferNull{ public static void main(String args[]){ Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> (); System.out.println("\nAdd null to the Deque"); myDeque.offer(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.offer(ConcurrentLinkedDeque.java:1012) at DequeOfferNull.main(DequeOfferNull.java:9)
3.
throws IllegalArgumentException if some property of the specified
element
prevents it from being added to this deque
No comments:
Post a Comment