Java. util. Queue usage

Source: Internet
Author: User

A queue is a special linear table. It can only be deleted at the front end of the table, but inserted at the back end of the table (rear. The end of the insert operation is called the end of the team, and the end of the delete operation is called the head of the team. When there are no elements in a queue, it is called an empty queue.

In a data structure such as a queue, the first element to be inserted will be the first element to be deleted; the last element to be inserted will be the last element to be deleted, therefore, a queue is also called a linear table of "FIFO-first in first out.

Added the java. util. Queue interface in java 5 to support common Queue operations. This interface extends the java. util. Collection interface.

When using Queue, try to avoid the add () and remove () methods of Collection. Instead, use offer () to add elements and poll () to obtain and remove elements. Their advantage is that the return value can be used to determine whether the operation is successful or not. The add () and remove () Methods throw an exception when the operation fails. If you want to use the front-end without removing this element, use
Element () or peek () method.

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

Package com. ljq. test; import java. util. using list; import java. util. queue; public class QueueTest {public static void main (String [] args) {// add () and remove () Methods throw an exception when they fail (not recommended) queue <String> queue = new queue list <String> (); // Add the Queue element. 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 for (String q: queue) {System. out. println (q);} System. out. println ("="); System. out. println ("element =" + queue. element (); // returns the first element for (String q: queue) {System. out. println (q);} System. out. println ("="); System. out. println ("peek =" + queue. peek (); // returns the first element for (String q: queue) {System. out. println (q );}}}

 

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.