Parsing queues in Java and simulating queues with LinkedList set

Description of queue in API:

The collection used to save the element before processing it. In addition to the basic collection operations, queues also provide 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 (null or false, depending on the operation). The latter form of insert operation is specially designed for queue implementation with capacity constraints; in most implementations, insert operation will not fail.

Queues usually (but not necessarily) sort elements in FIFO (first in, first out). However, priority queues and LIFO queues (or stacks), the former sort elements according to the natural order of the provided comparators or elements, and the latter sort elements according to LIFO (last in, first out) sorts the elements. Regardless of the sorting method used, the head of the queue is the element removed by calling remove() or poll(). In the FIFO queue, all new elements are inserted at the end of the queue. Other types of queues may use different element placement rules. Each queue implementation must specify its order property. If possible, the offer method can insert an element, otherwise it returns false. This is the same as collection Unlike the add method, this method can only fail to add elements by throwing unchecked exceptions. The offer method is designed for normal failures rather than exceptions, such as in a queue with a fixed (bounded) capacity. The remove () and poll () methods remove and return the header of the queue. Which element to remove from the queue is the function of the queue sorting policy, which is different in various implementations. The remove () and poll () methods behave differently only when the queue is empty: the remove () method throws an exception, while the poll () method returns null. Element () and Peek () return, but do not remove, the header of the queue. The queue interface does not define a method to block the queue, which is common in concurrent programming. The BlockingQueue interface defines methods that wait for elements to appear or for space to be available in the queue. These methods extend this interface. Queue implementations usually do not allow null elements to be inserted, Although some implementations (such as LinkedList) does not prohibit null insertion. Even in implementations that allow null, null should not be inserted into the queue, because null is also used as a special return value of the poll method, indicating that the queue does not contain elements. Queue implementations usually do not define the element based version of the equals and hashcode methods, but inherit the identity based version from the object class , because element based equality is not always well defined for queues with the same elements but different sort attributes.

Using queues in Java can be simulated with the LinkedList set

Method uses the LinkedList collection, and uses the APIs addlast, removefirst, isempty, etc. to collectively simulate queue operations

Queue

Out of queue

Air judgment

Sample code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>