Differnce between addfirst and offerFirst methods in ArrayDeque

后端 未结 2 1249
悲&欢浪女
悲&欢浪女 2021-01-04 19:23

Have tried out a sample program to understand the difference between addFirst and offerFirst methods in ArrayDeque of Java 6. But they

2条回答
  •  星月不相逢
    2021-01-04 20:01

    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.

提交回复
热议问题