Queue collection of Java collections

Source: Internet
Author: User

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.

Several methods are defined in the queue interface:

void Add (Object e): inserts the specified element into the tail of the queue.

object Element (): Gets the element of the queue header, but does not delete the element.

Boolean offer (Object e): inserts the specified element into the tail of this queue. This method is usually more effective than add (Object e) When using a queue with limited capacity.

Object Peek (): returns the element at the head of the queue, but does not delete the element. If the queue is empty, NULL is returned.

Object Poll (): returns the element of the queue header and deletes the element. If the queue is empty, NULL is returned.

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 also has a deque interface, deque represents a "double-ended queue", the double-ended queue can be removed from both ends at the same time or add elements, so deque can be used as a stack. Java provides the Arraydeque implementation class and the LinkedList implementation class for Deque.

1.PriorityQueue Implementation Class

Priorityqueue is a relatively standard queue implementation class, not an absolute standard. This is because the order in which the Priorityqueue saves the queue elements is not saved in the order in which the elements were added, but the size of the elements is sorted and then saved when the element is added. So using peek () or pool () in Priorityqueue to remove the elements from the head of the queue, not the elements that were first added, but the smallest elements.

public class Priorityqueuetest {public    static void Main (string[] args) {        Priorityqueue PQ = new Priorityqueue (); C5/>pq.offer (6);        Pq.add ( -3);        Pq.add (a);        Pq.offer ();        Output: [ -3, 6, +]        System.out.println (PQ);    }}

Priorityqueue does not allow the insertion of a null element, it also needs to sort the queue elements, priorityqueue in two ways:

Natural sorting: Elements in a naturally ordered Priorityqueue collection must implement the comparator interface, and should be multiple instances of a class, which could cause classcastexception exceptions.

Custom sort: When you create a priorityqueue queue, you pass in an comparable object that is responsible for sorting all the elements in all the queues. Using custom sorting does not require the comparator interface to be implemented.

2.Dueue interface and Arraydeque implementation class

The Deque interface is a sub-interface of the queue interface, which represents a double-ended queue , and Deque defines a number of methods:

void AddFirst (Object e): adds the specified element to the head of the double-ended queue.

void AddLast (Object e): adds the specified element to the tail of the double-ended queue.

Iteratord descendingitrator (): returns the iterator that corresponds to the double-ended queue that iterates through the elements in the queue in reverse order.

Object GetFirst (): gets but does not delete the first element of a double-ended queue.

Object getlast (): gets but does not delete the last element of a double-ended queue.

Boolean Offfirst (Object e): adds the specified element to the head of the double-ended queue.

Boolean offlast (OBject e): adds the specified element to the tail of the double-ended queue.

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

Object peeklast (): gets but does not delete the last element of the double-ended queue, or null if the double-ended queue is empty.

Object Pollfirst (): gets and removes the first element of a double-ended queue, or null if the double-ended queue is empty.

Object polllast (): gets and removes the last element of the double-ended queue, or null if the double-ended queue is empty.

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

void push (Object e) (Stack method): pushes an element into the top of the stack represented by the double-ended queue. Equivalent to AddFirst ().

Object Removefirst (): gets and deletes the first element of the double-ended queue.

Object Removefirstoccurence (Object o): removes the first occurrence of the double-ended queue of element O.

Object removelast (): gets and deletes the last element of the double-ended queue O.

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

public class Arraydequetest {public    static void Main (string[] args) {        Arraydeque queue = new Arraydeque ();        Queue.offer ("Spring");        Queue.offer ("Summer");        Queue.offer ("Autumn");        Output: [Spring, Summer, autumn]        System.out.println (queue);        Output: Spring        System.out.println (Queue.peek ());        Output: [Spring, Summer, autumn]        System.out.println (queue);        Output: Spring        System.out.println (Queue.poll ());        Output: [Summer, Autumn]        System.out.println (queue);            }}
Treat the double-ended queue as the stack public class Dequestack {public    static void Main (string[] args) {        Arraydeque stack = new Arraydeque (); C2/>stack.push ("Spring");        Stack.push ("Summer");        Stack.push ("Autumn");        Output: [Autumn, Summer, spring]        System.out.println (stack);        Output: Autumn        System.out.println (Stack.peek ());        Output: [Autumn, Summer, spring]        System.out.println (stack);        Output: Autumn        System.out.println (Stack.pop ());        Output: [Summer, Spring]        System.out.println (stack);    }}

3.LinkedList Implementation Class

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.

The implementation mechanism of LinkedList and Arraylist,arraydeque is completely different, and the elements in the collection are saved as arrays in ArrayList and Arraydeque, so there is good performance when accessing the collection elements randomly. While LinkedList saves the elements in the collection in the form of a list, the performance of random access to the collection elements is poor, but when inserting and deleting elements (just changing the address that the pointer refers to), you need to point out that Although Vector is also an array to store the collection but because it implements the thread synchronization (and the implementation of the mechanism is not good), so the performance of all aspects are relatively poor.

Queued Queue collection for Java 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.