Java Collection--queue Usage

Source: Internet
Author: User

A queue is a special linear table that allows for deletion only at the front end of the table (front), and in the back end of the table (rear). The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header. When there are no elements in the queue, it is called an empty queue.

In this data structure of the queue, the first element to be inserted will be the first to be deleted, whereas the last element inserted will be the last element to be deleted, so the queue is also known as the "FIFO" (Fifo-first in first out) linear table.

A new Java.util.Queue interface has been added in Java5 to support common operations on queues. The interface extends the Java.util.Collection interface.

To avoid collection's add () and remove () methods as much as possible, use the offer () to add elements and use poll () to get and move out of the element. Their advantage is that the return value can be used to determine success or failure, and the Add () and remove () methods throw exceptions when they fail. If you want to use the front end without moving the element, use the
Element () or peek () method.

It is worth noting that the LinkedList class implements the queue interface, so we can use LinkedList as a queue.

 Package Com.ljq.test;import Java.util.linkedlist;import Java.util.queue;public class Queuetest {public static voi D main (string[] args) {//add () and remove () methods throw exceptions when they fail (not recommended) queue<string> Queue = new Linkedlist<str        Ing> ();        add Element Queue.offer ("a");        Queue.offer ("B");        Queue.offer ("C");        Queue.offer ("D");        Queue.offer ("E");        for (String q:queue) {System.out.println (q);        } System.out.println ("= = ="); System.out.println ("poll=" +queue.poll ());        Returns the first element and deletes the for (String q:queue) {System.out.println (q) in the queue;        } System.out.println ("= = ="); System.out.println ("element=" +queue.element ());        Returns the first element for a (String q:queue) {System.out.println (q);        } System.out.println ("= = ="); System.out.println ("peek=" +queue.peek ());        Returns the first element for a (String q:queue) {System.out.println (q); }            }}

Java Collection--queue Usage

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.