Have tried out a sample program to understand the difference between addFirst and offerFirst methods in ArrayDeque of Java 6. But they seem to be same, any suggestions?
public void interfaceDequetest()
{
        try{
        ArrayDeque<String> ad = new ArrayDeque<String>();
        ad.addFirst("a1");
        ad.offerFirst("o1");
        ad.addFirst("a2");
        ad.offerFirst("02");
        ad.addFirst("a3");
        System.out.println("in finally block");
        for (String number : ad){
            System.out.println("Number = " + number);
        }
}
                The difference is what happens when the addition fails, due to a queue capacity restriction:
.addFirst() throws an (unchecked) exception,.offerFirst() returns false.This is documented in Deque, which ArrayDeque implements.
Of note is that ArrayDeque has no capacity restrictions, so basically .addFirst() will never throw an exception (and .offerFirst() will always return true); this is unlike, for instance, a LinkedBlockingQueue built with an initial capacity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With