Explanation of the queue in API:
Public Interface Queue <E> Extends Collection <E>
Collection used to preserve elements before processing elements. In addition to the basic collection operation, the queue also provides other insertion, extraction and inspection operations. There are two forms of each method: one throwing an abnormality (when the operation fails), and the other returns a special value (null or false, depending on the operation). The latter form of insert operation is designed for Queue, which specializes in capacity restrictions; in most implementation, the insertion operation will not fail.
The queue is usually (but not necessarily) to sort each element in the form of FIFO (advanced first). However, the priority queue and Lifo queue (or stack) exception, the former sorts the elements based on the natural order of the comparator or element, and the latter sorts the elements according to the LIFO (follow -up first). Regardless of which sorting method is used, the head of the queue is the element removed by calling () or poll (). In the FIFO queue, all new elements are inserted into the end of the queue. Other types of queues may use different element placement rules. Each queue implementation must specify its order attributes.
If possible, the offer method can be inserted into a element, otherwise it will return false. This is different from the Collection.Add method. This method can only fail by throwing out unpaid abnormalities. The offer method design is used for normal failure, not abnormal situations, such as in a queue of fixed (boundary).
Remove () and POLL () methods can be removed and returned to the head of the queue. In the end, which element is removed from the queue is the function of the queue sorting strategy, and this strategy is different in various implementations. Remove () and POLL () methods are only different when queue is empty: Remove () method throws an exception, and the Poll () method returns NULL.
Element () and peek () return, but not removed, the head of the queue.
The Queue interface does not define the method of blocking queue, and this is very common in concurrent programming. The BlockingQueue interface defines the method of available space in the appearance of waiting elements or waiting for the queue, which expands this interface.
Queue implementation is usually not allowed to insert NULL elements, although some implementation (such as LinkedList) cannot be prohibited from inserting NULL. Even in the implementation of NULL, NULL should not be inserted into Queue, because Null is also used as a special return value for the Poll method, indicating that the queue does not contain elements.
Queue implements element -based versions of Equals and HashCode methods, but inherited the identity -based version from the Object class, because for queues with the same elements but different sorting attributes, the equal nature of elements is not always the same nature. Define well.
Use a queue in Java to simulate with the LinkedList collection
Methods using LinkedList collection and using API collective simulation queue operations such
Queue
void addLast (e e); // Insert the element into the ending of this list
Queue
E removefirst (); // Remove and return the first element of the list
Be empty
boolean isempty (); // determine whether the queue is empty
For example code
Package corejavaone; Import Java.util.linkedList; Import Java.util.nosucheLementedException; Public Class SimulatingQueue {Privatelist er> queue = new linkedList <integer> (); public boolean isempty () {reTurn this.queue.Isempty () ;} Public Void ENQueue (Int Data) {this.queue.addlast (data);} Public int dequeue () Throws nocomentexception {Return This.queue.remove First ();} Public Static void main (string [] args) {simulatingQueue q = new simulatingQueue (); q.enqueue (1); q.enqueue (2); q.enqueue (3); while (! q.Isempty ()) {int data = q.dequeue (); sysem.out .println (data);}}}