Queue queues in Java

Source: Internet
Author: User

I. Overview

It is located under the Java.util package, declaration: PublicInterfacequeue<e> extends collection<e>

The collection used to hold the element before it is processed. In addition to the basic Collection operations, the queue provides additional insert, fetch, and check operations. Each method has two forms: one throws an exception (when the operation fails), and the other Returns a special value (null or false, depending on the action). The latter form of the insert operation is designed specifically for the capacity-constrained Queue implementation, and in most implementations, the insert operation does not fail.

Queues typically (but not necessarily) order individual elements in FIFO (first-in, in-order) manner. However, the priority queue and the LIFO queue (or stack) exception, which sorts elements according to the natural order of the provided comparer or element, which sorts the elements by LIFO (LIFO). Regardless of the sort method used, the header of the queue is the element that is called remove() or poll() removed. In the FIFO queue, all new elements are inserted at the end of the queue. Other kinds of queues may use different element placement rules. Each Queue implementation must specify its order properties.

If possible, the offer method can insert an element, otherwise it returns false. This differs from the Collection.add method in that the method can only fail to add an element by throwing an unchecked exception. The Offer method is designed for normal failure situations, not anomalies, such as in a fixed (bounded) capacity queue.

remove()And poll() methods to remove and return the header of the queue. Exactly which element is removed from the queue is the function of the queue sort policy, which is different in various implementations. The remove () and poll () methods behave differently only when the queue is empty: theRemove () method throws an exception, and the poll () method returns null .

element()And peek() returns, but does not remove, the head of the queue.

The queue interface does not define a method for blocking queues , which is common in concurrent programming. BlockingQueueinterfaces define methods that wait for an element to appear or wait for free space in the queue, and these methods extend this interface.

The Queue implementation generally does not allow the insertion of null elements, although some implementations (such as LinkedList ) do not prohibit the insertion of null. Even in implementations that allow Nulls, null should not be inserted into the queue because null is also used as a special return value for the poll method, indicating that the queue does not contain elements.

The Queue implementation typically does not define an element-based version of the equals and hashcode methods, but instead inherits the identity-based version from the Object class. Because element-based equality is not always well-defined for queues that have the same elements but have different sorting properties.


Second, the method

1,boolean add(e e)

inserts the specified element into this queue (if it is feasible immediately and does not violate the capacity limit), returns trueon success, and throws illegalstateexceptionif no space is currently available.

Designated by:
Collection<E> in the interface add .
Parameters:
e-the element to be added
Return:
true(according to Collection.add(E) the provisions)
Thrown:
IllegalStateException -If the element cannot be added due to capacity limitations at this time
ClassCastException -If the class of the specified element does not allow it to be added to this queue
NullPointerException -If the specified element is null and this queue does not allow null elements
IllegalArgumentException -If some properties of this element do not allow it to be added to this queue

2. Boolean Offer (E e) Inserting the specified element into this queue (if it is feasible immediately and without violating capacity limits), this method is generally preferable when using a capacity-constrained queue, add(E) which may not be able to insert elements, but only throws an exception.

Parameters:
e-the element to be added
Return:
Returns trueif the element was added to this queue, otherwise false
Thrown:
ClassCastException -If the class of the specified element does not allow it to be added to this queue
NullPointerException -If the specified element is null and this queue does not allow null elements
IllegalArgumentException -If some properties of this element are not allowed to add it Add to this queue

3, E Remove () Gets and removes the header of this queue. This method poll differs from the only difference in that this queue is empty and throws an exception.

Return:
Header of the queue
Thrown:
NoSuchElementException-If this queue is empty

5, $ Poll () 

gets and removes the header of this queue, and returns nullif this queue is empty.

Return:
The
header of the queue, which returns null if this queue is empty
5, E element ()  

gets, but does not remove the header of this queue. This method peek differs from the only difference in that this queue is empty and throws an exception.

Return:
Header of the queue
Thrown:
NoSuchElementException -If this queue is empty
6, E Peek ()  

Gets the header of this queue without removing it, or nullif this queue is empty.

Return:
The
header of this queue, or null if this queue is empty

Queue queues in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.