Java Collection Framework (iv)--queue, LinkedList, Priorityqueue

Source: Internet
Author: User
Tags joins

    • Queue interface

Queue is used to simulate the data structure of a queue, which usually refers to a "FIFO" container. The head of the queue is held in the queue for the longest time element, and the tail of the queue is saved in the shortest time element in the queue. The new element inserts (offer) to the end of the queue, and the Access Element (poll) operation returns the element at the head of the queue. Typically, queues do not tolerate random access to elements in the queue.

Several methods are defined in the queue interface:

    1. void Add (Object e); 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 e); Joins the specified element to the tail of this queue. This method is usually better than the Add (Object e) method when using a capacity-constrained queue.
    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 and deletes the element, or null if this queue is empty.
    6. Object Remove (); Gets the element of the queue header and deletes the element.

  The queue has two commonly used implementation classes :LinkedList and Priorityqueue, each of which describes the two implementation classes.

    • LinkedList

  The LinkedList class is a strange class , which is the implementation class of the list interface , which means that it is a list collection that can randomly access elements in the collection based on the index. In addition, LinkedList also implements the Deque interface , Deque interface is a sub-interface of the queue interface, it represents a two-way queue , deque interface defines some ways to operate the queue in two directions:

  1. void AddFirst (Object e); Inserts the specified element at the beginning of the bidirectional queue.
  2. void AddLast (Object e); Inserts the specified element at the end of the bidirectional queue.
  3. Iterator Descendingiterator (); Returns an iterator that corresponds to the bidirectional queue that iterates over the elements in the queue in reverse order.
  4. Object GetFirst (); Gets, but does not delete, the first element of a two-way queue.
  5. Object GetLast (); Gets, but does not delete, the last element of a two-way queue.
  6. Boolean Offerfirst (Object e);//Inserts the specified element at the beginning of the bidirectional queue
  7. Boolean offerlast (Object e);//Inserts the specified element at the end of the bidirectional queue
  8. Object Peekfirst (); Gets, but does not delete, the first element of a bidirectional queue, or null if this double-ended queue is empty.
  9. Object Peeklast (); Gets, but does not delete, the last element of the bidirectional queue, or null if this double-ended queue is empty.
  10. Object Pollfirst (); Gets and deletes the first element of a bidirectional queue, or null if this double-ended queue is empty.
  11. Object Polllast (); Gets and deletes the last element of the bidirectional queue, or returns null if this double-ended queue is empty.
  12. Object pop (); POPs out the first element in the stack represented by the bidirectional queue.
  13. void push (Object e); Push an element into the stack represented by the bidirectional queue.
  14. Object Removefirst (); Gets, and removes the first element of the bidirectional queue.
  15. Object Removefirstoccurrence (object e); Delete the first occurrence of the two-way queue element E.
  16. Removelast (); Gets and deletes the last element of the bidirectional queue.
  17. Removelastoccurrence (Object e); Delete the last occurrence of the two-way queue element E

From the above method can be seen,LinkedList not only can be used as a two-way queue, but also as a "stack" use , because the class also contains pop (out of the stack) and push (into the stack) two methods. In addition,LinkedList implements the list interface, so it is also used as a list.

Recommendations

    1. If you need to traverse the list collection element, for ArrayList, vector collections, you should use the random access Method (get) to iterate over the collection elements , which is better for the LinkedList collection, You should use Iterators (Iterater) to iterate through the collection elements.
    2. If you need to perform insert and delete operations frequently to change the size of the list collection, you should use the LinkedList collection instead of ArrayList. Using ArrayList, vector collections will require the size of the memory array to be redistributed frequently, with a time overhead that is often dozens of times times the cost of using LinkedList, with poor results.
    3. If you have multiple threads that need to access the elements in the list collection at the same time, consider using vector as a synchronous implementation.
    • Priorityqueue Implementation Class

  Priorityqueue is a relatively standard queue implementation class , which is said to be a more standard queue implementation, rather than an absolute standard queue implementation because: Priorityqueue the order in whichthe queue elements are saved is not in the order in which they are queued. Instead, it is reordered by the size of the queue element. So when the Peek method is called alive to pull the elements in the queue, the element that first enters the queue is not fetched, but the smallest element in the queue is taken out. in this sense, Priorityqueue has violated the most basic principles of the queue: FIFO. The following procedure demonstrates the use of the Priorityqueue queue.

 Public classTest { Public Static voidMain (string[] args) {Priorityqueue<Integer> PQ =NewPriorityqueue<integer>(); Pq.offer (3); Pq.offer (-6); Pq.offer (9); //printing results for [-6, 3, 9]System.out.println (PQ); //Print result is-6System.out.println (Pq.peek ()); //Print result is-6System.out.println (Pq.poll ()); }}

Priorityqueue does not allow the insertion of a null element, it also needs to sort the queue elements, and the queue elements are sorted in two ways: natural sorting, custom sorting , and the use of natural sort and custom sorting as in the previous TreeSet collection. Readers can view the Java Collection (ii) the contents of this chapter

Java Collection Framework (iv)--queue, LinkedList, Priorityqueue

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.