Java Collection Framework (14) Queue

Source: Internet
Author: User
Tags throw exception

I. Overview

A queue structure collection that stores the elements that will be processed. Typically, the elements are sorted in a FIFO manner, but this is not required. For example, the priority queue is an exception, which is sorted by the value of the element. But anyway, The implementation of each queue must specify its sorting properties. The queue typically does not define the equal and Hashcode methods of the element.

Two. Main methods

There are two forms of each queue method (1) The failure of an operation throws an exception (2) The operation fails to return a specific value, usually null or FALSE

Type of operation Throw exception return a specific value
Insert Add (E) Offer (e)
Remove Remove () Poll ()
Examine Element () Peek ()

The implementation of the queue may limit the number of elements stored by the collection, which is called a bounded queue, and for bounded queues when the Add method is called, the IllegalStateException exception is thrown if the number of elements exceeds its capacity limit. Offer method is specially designed for the bounded queue , and add is different when the insertion element fails, it returns false instead of throwing an exception.

Both the Remove and poll methods eject elements from the head of the queue. Specifically, the element is ejected, which depends on the queue's sorting strategy. Remove and poll only differ when it is an empty collection, and remove throws Nosuchelementexception, Instead, poll returns a null value.

Both the element and Peek methods return the header elements of the queue, but they are not removed from the queue. Same as remove and poll only when the empty collection is different, element throws Nosuchelementexception and peek returns a null value .

The implementation of the queue usually does not allow the insertion of null values, except for the exception of LinkedList, which allows the insertion of null values for historical reasons, but it is important to note that NULL is also the special value returned by the poll and Peek methods.

Three. Implement

The implementation of queue can be divided into common implementation and concurrency implementation

The general implementation of the main two, one is LinkedList, one is priorityqueue.linkedlist in the list we have said, it inherits from the queue interface, provides a FIFO queue operation form.

Priorityqueue is a stack-based prioritization queue that can be sorted according to the natural ordering of elements or a given sequencer.

A series of synchronous queue interfaces and real classes are included in the Java.util.concurrent package. Blockingqueue inherits from the queue, which waits for a non-empty queue to be available when retrieving elements, and changes the state to a usable state after an element is deposited. The following is its implementation class.

    • L Inkedeblockingqueue -optional bounded FIFO blocking queue based on link node
    • arrayblockingqueue -array-based, bounded FIFO-style blocking queue
    • priorityblockingqueue -based on the stack structure unbounded blocking queue
    • Span style= "FONT-SIZE:16PX; Font-family: Young Round "> delayqueue - Time dispatch queue based on stack structure
    • sychronousqueue -queue by using a simple docking mechanism of the Blockingqueue interface

In JDK7, Transferqueue is a special blockingqueue that, after adding an element to the queue, can choose to be in a wait (blocking) state to have another thread retrieve the element, Transferqueue only one implementation class

    • linkedtransferqueue-unbounded transferqueue based on link nodes

Java Collection Framework (14) Queue

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.