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 );}}}