Queue in java

Source: Internet
Author: User

Queue in java

I. Overview

It is located under the java. util package and declares: public interfaceQueue Extends Collection

The collection used to save the elements before processing the elements. Besides the basicCollectionIn addition, the queue also provides other insert, extract, and check operations. Each method has two forms: one is to throw an exception (when the operation fails), and the other is to return a special value (NullOrFalseDepends on the operation ). The latter form of the insert operation is used for capacity restrictions.QueueImplementation; in most implementations, the insert operation will not fail.

Generally (but not necessarily) queues sort each element in FIFO (first-in-first-out) mode. Except for the priority queue and the LIFO Queue (or stack), the former sorts the elements based on the provided comparator or the natural sequence of the elements, and the latter orders the elements based on the LIFO (later, first, first, and foremost) to sort the elements. No matter which sort method is usedHeaderAll are calledremove()Orpoll()The removed element. In a FIFO queue, all new elements are inserted into the queue'sEnd. Other types of queues may use different element placement rules. EachQueueThe implementation must specify its ordered attribute.

If possible,offerMethod to insert an element. Otherwise, an error is returned.False. This correspondsCollection.addDifferent Methods: This method can only cause element addition failure by throwing an unchecked exception.OfferThe method is designed for normal failures rather than exceptions, for example, in a queue with a fixed capacity (bounded.

remove()Andpoll()Method to remove and return the queue headers. Which of the following elements is removed from a queue is a queue sorting policy, which is different in various implementations.Remove ()AndPoll ()The behavior of the method is different only when the queue is empty:Remove ()Method throws an exception, whilePoll ()Method returnsNull.

element()Andpeek()Returned, but not removed, the queue header.

QueueInterface is not definedBlocking queueAnd this is very common in concurrent programming.BlockingQueueInterfaces define methods that wait for elements to appear or wait for queues to have available space. These methods extend this interface.

QueueInsert is usually not allowed.NullElement, although some implementations (suchLinkedList) Insert is not prohibited.Null. Even if null is allowedNullInsertQueueBecauseNullAlso usedPollA special return value of the method, indicating that the queue does not contain elements.

QueueImplementation is usually not definedEqualsAndHashCodeThe element-based version of the method, insteadObjectThe class inherits the identity-based version, because element-based equality is not always well defined for Queues with the same elements but different sorting attributes.


Ii. Method

1. booleanAdd(E)

Insert the specified element into this queue (if it is feasible immediately and does not violate the capacity limit), return if it is successful TrueIf no available space exists IllegalStateException.

Specified:Interface Collection In add Parameters: e-Elements to be added Return Value: True(According Collection.add(E)) Throw: IllegalStateException-This element cannot be added due to capacity limitations. ClassCastException-If the class of the specified element cannot be added to this queue NullPointerException-If the specified element is null and the queue does not allow null Elements IllegalArgumentException-If some attributes of this element cannot be added to this queue
2. boolean Offer(E) Insert the specified element into this queue (if immediately feasible and does not violate the capacity limit), this method is generally better add(E)The latter may not be able to insert elements, but only throw an exception.

Parameters: e-Elements to be added Return Value:If this element has been added to this queue, return TrueOtherwise False Throw: ClassCastException-If the class of the specified element cannot be added to this queue NullPointerException-If the specified element is null and the queue does not allow null Elements IllegalArgumentException-If some attributes of this element cannot be added to this queue
3. E Remove() Get and remove the queue header. This method works pollThe only difference is that an exception is thrown when the queue is empty.

Return Value:Queue Header Throw: NoSuchElementException-If this queue is empty
4. E Poll()

Gets and removes the queue header. If this queue is empty, the system returns Null.

Return Value:Queue header. If this queue is empty, return Null5. E Element()

Get, but do not remove the header of this queue. This method works peekThe only difference is that an exception is thrown when the queue is empty.

Return Value:Queue Header Throw: NoSuchElementException-If this queue is empty Peek()

Obtains but does not remove the header of this queue. If this queue is empty, return Null.

Return Value:The header of this queue. If this queue is empty, return Null

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.