Java LinkedBlockingDeque clear() 方法

2025 年 3 月 21 日 | 阅读 2 分钟

LinkedBlockingDeque 类的 clear() 方法会自动移除此双端队列中的所有元素。调用此方法后,该双端队列将变为空。

语法

参数

不适用

指定者:

LinkedBlockingDeque 类的 clear() 方法是在 Collection<E> 接口中定义的 clear() 方法。

Override

LinkedBlockingDeque 类的 clear() 方法重写了 AbstractQueue<E> 类的 clear() 方法。

示例 1

输出

Deque : [23, 123, 12]
After applying clear() method :  
Deque : []

示例 2

输出

1. Sugar
2. Salt
3. Potato
4. Rice
Is Your shopping done.?
true/false : Your queue is empty.
Make a new list.

示例 3

输出

Exception in thread "main" java.lang.NullPointerException
	at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357)
	at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334)
	at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
	at com.
  • getFirst()

    Java LinkedBlockingDeque method The method of LinkedBlockingDeque class returns the first element of this deque but does not withdraw it. Syntax: public E Parameters: NA Specified By: The method of LinkedBlockingDeque class is specified by method in interface Deque&lt;E&gt;. Return Value: The method returns the first element of...

    3 min read

  • iterator()

    Java LinkedBlockingDeque method The method of LinkedBlockingDeque class returns an iterator across the elements of this deque in the appropriate order. The elements returned will be in the order from head to tail. Syntax: public Iterator&lt;E&gt; Parameters: NA Specified By: The method of LinkedBlockingDeque class is specified by: ...

    3 min read

  • pollLast()

    Java LinkedBlockingDeque Method The method of LinkedBlockingDeque class retrieves the last element of this deque, and also removes it. This method returns null if this deque is empty. The pollLast(long timeout, TimeUnit unit) method of LinkedBlockingDeque class retrieves the last element of this deque, awaiting...

    2 min read

  • pollFirst()

    Java LinkedBlockingDeque Method The method of LinkedBlockingDeque class retrieves the first element of this deque, and also removes it. The method returns null if this deque is empty. The pollFirst(long timeout, TimeUnit unit) method of LinkedBlockingDeque class retrieves the first element of this deque, awaiting...

    2 min read

  • element()

    Java LinkedBlockingDeque method The method of LinkedBlockingDeque class returns the head of the queue corresponding to this deque but does not withdraw it. The result returned by this method is similar to the one returned by getFirst() method. Syntax: public E Parameters: NA Specified By: The method of...

    3 min read

  • pop()

    Java LinkedBlockingDeque Method The method of LinkedBlockingDeque class returns the first element of this deque, and also removes it. The result returned by method is similar to that returned by Deque.removeFirst(). Syntax: public E Parameters: NA Specified By: The method of LinkedBlockingDeque class is specified by ...

    2 min read

  • peekLast()

    Java LinkedBlockingDeque Method The method of LinkedBlockingDeque class retrieves this deque&#39;s last element, but doesn&#39;t remove it. The method returns null if this deque is empty. Syntax: public E peekLast () Parameters: NA Specified By: The peekLast () method of LinkedBlockingDeque class is specified by method in interface Deque&lt;E&gt;. Return...

    3 min read

  • add()

    Java LinkedBlockingDeque method The method of LinkedBlockingDeque class inserts the specified element at the tail of this LinkedBlockingDeque. The method overrides the add in class AbstractQueue&lt;E&gt; Syntax: public boolean add(E e) Parameters: e- It is the element to add Specified By: The method of ConcurrentLinkedQueue class is specified...

    2 min read

  • drainTo()

    Java LinkedBlockingDeque Method The method of LinkedBlockingDeque() class removes all the elements present in this queue and adds them to the provided collection. Syntax: public int drainTo(Collection&lt;? super E&gt; c) public int drainTo(Collection&lt;? super E&gt; c, int maxElements) Parameters: c - this is the collection to which elements are...

    3 min read