Java Collection Queue details and uses

Source: Internet
Author: User

Queue is a common data structure whose main feature is FIFO (first in, out), in which the queue in Java is defined:

Public interface Queue<e> extends collection<e> {    E element ();    Boolean offer (E O);    E Peek ();    E poll ();    E Remove ();


Although the queue has a FIFO feature, but the specific output of which element, the various implementations of the queue are different, especially in the case of sorting, the new INPUT element is not placed at the end of the queue, but placed in the appropriate location. Each implementation of the queue must specify a sort attribute (ordering properties).

The queue may have a limit on the number of elements deposited. Such a queue becomes "bounded" (bounded), some queue implementations in Java.util.concurrent are bounded, and the queue implementations in Java.util are not bounded.

There are two methods for each operation of the queue:


Queue Interface Structure
  T Hrows exception Returns Special value
insert Add (E) offer (e)
Remove /td> Remove () poll ()
examine Element () Peek ()

The



Add method inherits from collection, and when the element of the queue has reached the limit number, add throws a IllegalStateException exception; The offer method is only positioned in the case where the bounded queue is applied. The offer returns False when the queue is fully loaded. The

Remove and poll methods both delete and return the head element in the queue (note that the first element is not inserted because some queue implementations are sorted). When the queue is empty, remove throws a Nosuchelementexception exception, and poll returns NULL.

Element and peek return but do not delete the header elements in the queue, and they differ like remove and poll. The implementation of the

queue generally does not allow the insertion of NULL, only LinkedList is an exception, it allows the insertion of NULL, but the consumer must be careful not to confuse the null value returned by the poll and Peek methods. The implementation of the

queue generally does not define an element-based equals and hashcode method, but instead calls a corresponding method that inherits from object. The

Queue interface does not define blocking methods commonly used in parallel programs, that is, the element does not have to check for sufficient space in the queue before it enters the queue. However, these methods are defined as the Java.util.concurrent.BlockingQueue interface for the queue extension.

The normal LinkedList implementation does not define a special sorting algorithm, so the output element is in the order of insertion:

Import java.util.*;p Ublic class Countdown {public    static void Main (string[] args)            throws interruptedexception {        int time = ten;        queue<integer> queue = new linkedlist<integer> ();        for (int i = time; I >= 0; i--)            queue.add (i);        while (!queue.isempty ()) {            System.out.println (Queue.remove ());            Thread.Sleep (+);}}}    


The output of the program is:
10
9
8
.....

If you make a small change, use Priorityqueue

Import java.util.*;p Ublic class Countdown {public    static void Main (string[] args)            throws interruptedexception {        int time = ten;        queue<integer> queue = new linkedlist<integer> ();        queue<integer> pqueue = new priorityqueue<integer> ();        for (int i = time; I >= 0; i--)            queue.add (i);        while (!queue.isempty ()) {            Pqueue.add (Queue.remove ());                   }        while (!pqueue.isempty ()) {            System.out.println (Pqueue.remove ());}}}    


The output is:
0
1
2
......

Looking at the document, the Priorityqueue's interior is a min heap, in fact, the output of observing priorityqueue can also be found:

        int time = ten;        queue<integer> queue = new linkedlist<integer> ();        queue<integer> pqueue = new priorityqueue<integer> ();        for (int i = time; I >= 0; i--)            queue.add (i);        while (!queue.isempty ()) {            Pqueue.add (Queue.remove ());                   }        System.out.println (pqueue);        Pqueue.remove ();        System.out.println (pqueue); the output is [0,1,5,4,2,9,6,10,7,8,3][1,2,5,4,3,9,6,10,7,8] is actually two min-heap according to the output from top to bottom, from left to right.

Java Collection Queue details and uses

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.