Java Collection Queue specific explanations 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 details of which element to output, 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 position. Each implementation of the queue must specify a sort attribute (ordering properties).

The queue may have a limit on the number of elements deposited.

This 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
Throws exception Returns Special Value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()



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



Both the Remove and poll methods 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 their differences are similar to remove and poll.

Queue implementation generally does not allow the insertion of NULL, only LinkedList is an accident, it allows the insertion of NULL, but the user must be aware. Do not confuse the null value returned by the poll and Peek methods.



The implementation of the queue generally does not define the element-based equals and hashcode methods, but rather calls the corresponding methods that inherit from object.

The queue interface does not define the blocking methods that are frequently used in parallel programs, meaning that elements do not have to check for sufficient space in the queue before they enter the queue. These methods are defined only 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 will follow 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
.....

Suppose to make a small change, using 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
......

Check the documentation. The interior of the Priorityqueue is a min heap. In fact, observing the output of Priorityqueue can also find this:

        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 specific explanations 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.