java--(vi) collection's queue collection

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Queue Collection

  The queue collection is used to simulate the data structure of a queue, which usually refers to a "FIFO" container. Queue

The head holds the longest element in the queue, and the tail of the queue holds the element that holds the shortest time in the queue. sgd

Element insertion (offer) to the end of the queue, the Access Element (poll) operation returns the elements of the queue header. Usually the queue does not allow

Randomly accesses elements in the queue.

Several methods are defined in the queue interface:

1) void Add (Object o): joins the specified element to the tail of this queue.

2) Object element (): Gets the element of the queue header, but does not delete the element.

3) Boolean offer (Object O): Adds the specified element to the tail of this queue. When there was also a capacity-constrained queue,

This method is usually better than the add (Object O) method

4) Object Peek (): Gets the element of the queue header, but does not delete the element and returns null if this queue is empty.

5) Object poll (): Gets the element of the queue header, deletes the element, and returns null if this queue is empty.

6) Object Remove (): Gets the element of the queue header and deletes the element.

The queue interface has a Priorityqueue implementation class. In addition, the queue has a deque interface, and its implementation class

You can use it as a queue or as a stack. Java provides deque with two implementation classes for Arraydeque and LinkedList .

1.PriorityQueue Implementation Class

Priorityqueue is a relatively standard queue implementation class. Priorityqueue the order in which the queue elements are saved is not added by

In the order of queues, 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. Here's a demonstration.

The usage of the Priorityqueue queue.

 import   Java.util.PriorityQueue;  public  class   Priorityqueuetest { public  static  void   main (string[] args) {Priori                Tyqueue PQ  = new   Priorityqueue ();        Pq.offer ( 6);        Pq.offer ( -3);        Pq.offer ( 9);        Pq.offer ( 0);        System.out.println (PQ);        System.out.println (Pq.poll ());    System.out.println (Pq.peek ()); }}

Operation Result:

[ -3, 0, 9, 6]-30

2.Deque interface and Arraydeque implementation class

The Deque interface is a sub-interface of the queue interface, which represents a double-ended queue, and the Deque interface defines some double-ended queues

Method of:

1) void AddFirst (Object o): Inserts the specified element at the beginning of the double-ended queue.

2) void AddLast (Object o): Inserts the specified element at the end of the double-ended queue.

3) Iterator descendingiterator (): Returns the iterator that corresponds to the double-ended queue, which iterates over the elements in the team column in reverse order.

4) Object GetFirst (): Gets but does not delete the first element of a double-ended queue.
5) Object GetLast (): Gets but does not delete the last element of the double-ended queue.

6) Boolean offerfirst (Object O): Inserts the specified element at the beginning of the double-ended queue.
7) Boolean offerlast (Object O): Inserts the specified element at the end of the double-ended queue.

8) Object Peekfirst (): Gets but does not delete the first element of the double-ended queue, or null if this double-ended queue is empty.

9) Object Peeklast (): Gets but does not delete the last element of the double-ended queue, or null if this double-ended queue is empty.

Object Pollfirst (): Gets and deletes the first element of the double-ended queue, or null if this double-ended queue is empty.

One) Object polllast (): Gets and deletes the last element of the double-ended queue, or null if this double-ended queue is empty.

The Object pop () (Stack method):p op out the top element of the stack represented by the double-ended queue. Equivalent to Removefirst ().

void push (Object o) (Stack method): Pushes an element into the top of the stack represented by the double-ended queue. Equivalent to AddFirst (o).
() Object Removefirst (): Gets and deletes the first element of the double-ended queue.

() object Removefirstoccurrence (Object o): Removes the first occurrence of the double-ended queue of element O.
Removelast (): Deletes the last element of the double-ended queue.

Removefirstoccurrence (Object o): Removes the last occurrence of the double-ended queue of element O.

The Deque interface provides a typical implementation class: Arraydeque, a double-ended queue based on a digital implementation, when creating deque

You can specify a numelement parameter that specifies the length of the object[] array, and if you do not specify a numelement parameter,

Deque the length of the underlying array is 16.

Hint: the implementation mechanism of the two collection classes of ArrayList and Arraydeque are basically similar, and their bottom-up is based on a dynamic, redistributed

Object[] Array to store the collection element, and when the collection element is outside the container of the array, a object[] array is reassigned at the bottom

To store the collection elements.

2.LinkedList Implementation Class

The LinkedList class is the implementation class for the list interface, and the LinkedList also implements the Deque interface. The following procedure demonstrates the LinkedList set

And the use of the combined.

1 Importjava.util.LinkedList;2 3 4  Public classLinkedlisttest {5 6      Public Static voidMain (string[] args) {7 8LinkedList lllinkedlist =NewLinkedlist<>();9         //joins a string element to the tail of a queueTenLllinkedlist.offer ("Dark Horse Programmer"); One         //adds a string element to the stack top of the stack ALllinkedlist.push ("Preach Intelligence podcast"); -         //adds a string element to the head of the queue, equivalent to the top of the stack -Lllinkedlist.offerfirst ("CSDN"); the          -          for(inti = 0; I < lllinkedlist.size (); i++) { - System.out.println (Lllinkedlist.get (i)); -         } +         //access without deleting the top element of the stack - System.out.println (Lllinkedlist.peekfirst ()); +         //accesses but does not delete the last element of a queue A System.out.println (Lllinkedlist.peeklast ()); at         //pops the top element of the stack to the "stack" - System.out.println (Lllinkedlist.pop ()); -         //you can see that the first element in the queue is deleted - System.out.println (lllinkedlist); -         //Access and delete the last element of a queue - System.out.println (Lllinkedlist.polllast ()); in System.out.println (lllinkedlist); -     } to  +}

Operation Result:

CSDN Podcast Black Horse programmer csdn Dark Horse programmer csdn[, Dark Horse programmer] Dark Horse programmer [Preach Wisdom Podcast]

LinkedList and ArrayList, arraydeque implementation mechanism is completely different, ArrayList, Arraydeque internal

Saves the elements in the collection as an array, so there is good performance when accessing the collection elements randomly, while the LinkedList internal

The form of a list to hold elements in the collection, so poor performance when randomly accessing collection elements, but performance when inserting, deleting elements

Very good (just change the address that the pointer refers to). It should be noted that although vectors are also stored as arrays

collection element, but because he implements the thread synchronization feature, all aspects of performance have been degraded.

java--(vi) Collection Queue collection

Related Article

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.