boolean
offerFirst(E e)
Inserts
the specified element at the front of this deque. Return true if the
element was added to this deque, else false.
import java.util.*; import java.util.concurrent.LinkedBlockingDeque; class DequeOfferFirst{ public static void main(String args[]){ Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5); System.out.println("Is 10 added to the Deque " + myDeque.offerFirst(10)); System.out.println("Elements in Deque are\n" + myDeque); System.out.println("\nIs 20 added to the Deque " + myDeque.offerFirst(20)); System.out.println("Elements in Deque are\n" + myDeque); System.out.println("\nIs 30 added to the Deque " + myDeque.offerFirst(30)); System.out.println("Elements in Deque are\n" + myDeque); System.out.println("\nIs 40 added to the Deque " + myDeque.offerFirst(40)); System.out.println("Elements in Deque are\n" + myDeque); System.out.println("\nIs 50 added to the Deque " + myDeque.offerFirst(50)); System.out.println("Elements in Deque are\n" + myDeque); System.out.println("\nIs 60 added to the Deque " + myDeque.offerFirst(60)); System.out.println("Elements in Deque are\n" + 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 [20, 10] Is 30 added to the Deque true Elements in Deque are [30, 20, 10] Is 40 added to the Deque true Elements in Deque are [40, 30, 20, 10] Is 50 added to the Deque true Elements in Deque are [50, 40, 30, 20, 10] Is 60 added to the Deque false Elements in Deque are [50, 40, 30, 20, 10]
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 DequeOfferFirstNull{ public static void main(String args[]){ Deque<Integer> myDeque = new ConcurrentLinkedDeque<Integer> (); System.out.println("\nAdd null to the Deque"); myDeque.offerFirst(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(Unknown Source) at java.util.concurrent.ConcurrentLinkedDeque.linkFirst(Unknown Source) at java.util.concurrent.ConcurrentLinkedDeque.offerFirst(Unknown Source) at DequeOfferFirstNull.main(DequeOfferFirstNull.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