Use of the Java Squadron column

Source: Internet
Author: User

I just saw a friend in the group asking about queue usage, which is really very seldom used in the actual writing code, but I never used it. It's just learning about data structures.

The following is a sample I wrote, I hope there are shortcomings, please put forward to correct. O (∩_∩) o~

Look at the code:

Import Java.util.linkedlist;import java.util.queue;public class testqueue{/** * @param args * @author Javaalpha * Info Test queue */public static void main (string[] args) {queue<string> queue = new linkedlist<string> (); Queue.offer ("1 ");//Insert an element Queue.offer (" 2 "), Queue.offer (" 3 "),///print the number of elements System.out.println (" Queue.size () "+queue.size ());// Traverse prints all elements, install insert is sequential print for (string string:queue) {System.out.println (string);}}} Printed content: Queue.size () 3123

The following is a description of the queue in the API:

Queue<e>
   
     
Extends collection<e>

The collection used to hold the element before it is processed. In addition to the main Collection operations, the queue provides additional insert, fetch, and check operations. Each method has two forms: one that throws an exception (when the operation fails), and one that returns a special value (null or false, depending on the operation). 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.

Throw exception Return special values
Insert add(e) offer(e)
Removed from remove() poll()
Check element() peek()

Queues typically, but not necessarily, order individual elements in FIFO (first-in, in-order) manner. Only the priority queue and the LIFO queue (or stack) exception, which sorts the elements according to the natural order of the supplied comparison or element, which sorts the elements in 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.

Suppose possible, offer a method can insert an element, otherwise it returns false. This Collection.add method differs from the method by simply throwing an unchecked exception and failing to join the element. 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 typically does not agree to insert a null element, although some implementations (such as LinkedList ) do not prohibit the insertion of null. Even in implementations that agree to NULL, 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 include elements.

The Queue implementation typically does not define the element-based version number of the equals and hashcode methods, but inherits the identity-based version number 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.

Use of the Java Squadron column

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.