Java set: Queue set; java set: queue

Source: Internet
Author: User

Java set: Queue set; java set: queue

What is a Queue set?

A:Queue is used to simulate the data structure of a Queue. A queue is usually a container of "first-in-first-out (FIFO. The queue header stores the elements with the longest storage time in the queue and the elements with the lowest storage time in the tail. Insert the new element to the end of the queue. The elements in the queue header are returned. Generally, the queue does not allow random access to elements in the queue.

The Queue interface defines the following methods:

Void add (Object e ):Insert the specified element to the end of the queue.

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

Boolean offer (Object e ):Insert the specified element to the end of the queue. This method is generally more effective than add (Object e) when a queue with limited capacity is used.

Object peek ():The element in the queue header is returned, but this element is not deleted. If the queue is empty, null is returned.

Object poll ():Return the element in the queue header and delete it. If the queue is empty, null is returned.

Object remove ():Obtain the element of the queue header and delete it.

The Queue interface has a PriorityQueue implementation class. In addition, Queue also has a Deque interface. Deque represents a "double-ended Queue". The double-ended Queue can delete or add elements from both ends at the same time, So Deque can be used as a stack. Java provides ArrayDeque implementation classes and javaslist implementation classes for Deque.

1. PriorityQueue implementation class

PriorityQueue is a standard queue implementation class, rather than an absolute standard. This is because the order in which PriorityQueue stores queue elements is not stored in the order in which elements are added, but stored after the elements are sorted by size. Therefore, in PriorityQueue, you can use peek () or pool () to retrieve the Header element of the queue. Instead of adding the first element, you can retrieve the smallest element.

Public class PriorityQueueTest {public static void main (String [] args) {PriorityQueue pq = new PriorityQueue (); pq. offer (6); pq. add (-3); pq. add (20); pq. offer (18); // output: [-3, 6, 20, 18] System. out. println (pq );}}

PriorityQueue cannot insert null elements. It also needs to sort queue elements. PriorityQueue has two sorting methods:

Natural sorting: the elements in the PriorityQueue set that uses natural sorting must implement the Comparator interface and must be multiple instances of a class. Otherwise, ClassCastException may occur.

Custom sorting: When a PriorityQueue queue is created, a Comparable object is input, which sorts all elements in all queues. The Comparator interface is not required for custom sorting.

2. Dueue interface and ArrayDeque implementation class

 The Deque interface is a sub-interface of the Queue interface, which represents a dual-end Queue, Deque defines some methods:

Void addFirst (Object e ):Add the specified element to the header of the double-end queue.

Void addLast (Object E ):Add the specified element to the end of the double-end queue.

Iteratord descendingItrator ():Return the iterator corresponding to the double-end queue. The iterator iterates the elements in the queue in reverse order.

Object getFirst ():Obtain but do not delete the first element of the dual-end queue.

Object getLast ():Obtain but not delete the last element of the dual-end queue.

Boolean offFirst (Object e ):Add the specified element to the header of the double-end queue.

Boolean offLast (OBject e ):Add the specified element to the end of the double-end queue.

Object peekFirst ():Obtain but not delete the first element of the double-ended queue. If the double-ended queue is empty, null is returned.

Object PeekLast ():Obtain but not delete the last element of the double-ended queue. If the double-ended queue is empty, null is returned.

Object pollFirst ():Obtain and delete the first element of the double-ended queue. If the double-ended queue is empty, null is returned.

Object pollLast ():Obtain and delete the last element of the double-ended queue. If the double-ended queue is empty, null is returned.

Object pop () (stack method ):Pop is the top element of the stack represented by the double-end queue. It is equivalent to removeFirst ().

Void push (Object e) (stack method ):Push an element to the top of the stack represented by the double-end queue. It is equivalent to addFirst ().

Object removeFirst ():Obtain and delete the first element of the double-end queue.

Object removeFirstOccurence (Object o ):Delete the first occurrence element o of the double-end queue.

Object removeLast ():Obtain and delete the last element o of the double-end queue.

Object removeLastOccurence (Object o ):Delete the last element o of the double-end queue.

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, and autumn] System. out. println (queue); // output: spring System. out. println (queue. poll (); // output: [summer, autumn] System. out. println (queue );}}
// Use the double-end queue as the public class DequeStack {public static void main (String [] args) {ArrayDeque stack = new ArrayDeque (); 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. javaslist implementation class

The listlist interface is an implementation class of the List interface. Therefore, it can be a set that allows Random Access to elements in the Set Based on indexes. In addition, it is also an implementation class of the Duque interface, so it can also be used as a dual-end queue or stack.

The implementation mechanisms of collections list and ArrayList and ArrayDeque are completely different. ArrayList and ArrayDeque store the elements in the set in an array. Therefore, random access to the set elements has better performance; the sorted list stores the elements in the set in the form of a linked list. Therefore, the random access to the set elements has poor performance, but the insertion and deletion of elements have excellent performance (you only need to change the address indicated by the pointer ), it should be pointed out that although Vector stores collections in the form of arrays, it achieves thread synchronization (and the implementation mechanism is poor), so the performance in all aspects is relatively poor.

 

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.