"Java Improvement"---queue collection

Source: Internet
Author: User
Tags comparable throw exception

Queue Collection

What is a queue collection?

A: Queue isused to simulate the data structure of a queue. Queues are usually "FIFO" containers. The head of the queue is kept in the queue for the longest-held element, and the tail holds the element with the shortest storage time.

The new element is inserted at the end of the queue, and the element that is fetched returns the queue header. Typically, queues do not allow random access to elements in the queue.

I. Understanding the queue1, Queue method Introduction

From the above, the queue interface inherits from collection, which is used to indicate that the inner element has a sequential collection. In addition to the basic collection operations, the queue provides additional insert, delete, and check operations. The queue interface is defined as follows:

 Public Interface extends Collection<e> {        E element ();         Boolean Offer (e e);        E Peek ();        E poll ();        E remove ();  

Each of the queue-related methods provides two forms: one that throws an exception if the operation fails, and one that returns a special value if the operation fails (null or FALSE).

The queue interface structure looks like this:

Operation

Throw exception

Return special values

Insert

Add (E)

Offer (e)

Removed from

Remove ()

Poll ()

Check

Element ()

Peek ()

(1). Add (E), offer (e) added at the tail:

What they have in common is that the implementation class is forbidden to add null elements, otherwise the null pointer nullpointerexception will be reported;

The difference is that the Add () method will report some run-time error errors when adding a failure (such as a queue full), and the Offer () method will not collapse even if the add fails, and will only return false.

(2) Remove (), poll () Delete and return to the head

The Remove () method will report nosuchelementexception error when the queue is empty; The poll () will not collapse, only return null.

(3) Element (), peek () gets but does not delete

The element () throws an exception when the queue is empty, and peek () does not collapse, only returns NULL.

2. Other

(1) Although linkedlist is not forbidden to add null, in general, the implementation class of the Queue does not allow the addition of a NULL element, because poll (), the Peek () method will return null at the time of the exception, you add null, when the acquisition is not good to tell whether Returned correctly.

(2) The queue is generally FIFO, but there are exceptions, such as priority queue priorities (which order is based on natural sorting or custom comparator), such as the LIFO queue (as in the stack, and then go out first).

(3) Regardless of the order of entry and exit, using the Remove (), the poll () method operations are the elements of the head, while the inserted position is not necessarily at the end of the queue, different queues will have different insertion logic.

Second, Priorityqueue realization class

Priorityqueue is a relatively standard queue implementation class. Priorityqueue The order in which the queue elements are saved is not in the order in which they are queued, but is rearranged by the size of the queue elements . So when the peek () method is called or the poll () method is removed, the team

The element in the column is not taken out of the first entry into the queue, but the smallest element in the queue is taken out.

1, Priorityqueue of the sorting method

The elements in Priorityqueue can be naturally sorted by default (that is, the numbers are small by default in the queue header, strings are sorted in dictionary order) or by the provided comparator (comparer) that are specified when the queue is instantiated.

(1) Small case

Priorityqueue<integer> Qi =NewPriorityqueue<integer>(); Qi.offer (5); Qi.offer (2); Qi.offer (1); Qi.offer (10); Qi.offer (3);  while(!Qi.isempty ()) {System.out.print (Qi.poll ()+ ",");        } System.out.println (); //in descending order, the smaller the line, the lower the end of the queue.Comparator<integer> CMP =NewComparator<integer>() {           Public intCompare (integer e1, integer e2) {returnE2-E1;        }        }; //here is the initial capacity 3, when we have more than 3 will automatically expand, so that it is a borderless containerpriorityqueue<integer> q2 =NewPriorityqueue<integer> (3, CMP); Q2.offer (2); Q2.offer (8); Q2.offer (9); Q2.offer (1);  while(!Q2.isempty ()) {System.out.print (Q2.poll ()+ ","); }

Output Result:

As you can see, by default Priorityqueue uses natural sorting. When specifying comparator, the Priorityqueue takes the specified sort.

Note : When comparator is not specified in Priorityqueue, the element that joins the Priorityqueue must implement the comparable interface (that is, the element is comparable), otherwise it will cause ClassCastException.

(For example, if you put an object, you must specify comparator because the object cannot be compared.)

2. Priorityqueue characteristics

(1) Queue elements are sorted by nature or by specific comparators

(2) If the initial capacity is not specified when instantiating, the default capacity is 11

(3) Automatic expansion. If the capacity is less than 64, twice times the growth of expansion; otherwise 50%

(4) No boundary container

(5) NULL element not supported

(6) Non-thread safe

(7) Support serialization

(8) Time Complexity O (log (n)) to queue up

About Dueue interfaces with Arraydeque implementation classes and LinkedList implementation classes are used later to write them again.

Now just know:

(1) The Deque interface is a sub-interface of the queue interface, which represents a double-ended queue.

(2) LinkedList is the implementation class of the list interface, so it can be a collection that can randomly access elements in the collection based on the index. In addition, it is an implementation class for the Duque interface, so it can also be used as a double-ended queue, or as a stack.

Think too much, do too little, the middle of the gap is trouble. Want to have no trouble, either don't think, or do more. Major, "13."

"Java Improvement"---queue collection

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.