Note: This is just an interface.
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 Advantages
The point is to determine whether the operation is successful or not through the return value. 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.
[Java]View
Plaincopy
- Example:
- /**
- *
- * @ Author Zang XT
- */
- Import java. util. Queue;
- Import java. util. Collections list;
- Public class testqueue {
- Public static void main (string [] ARGs ){
- Queue <string> queue = new queue list <string> ();
- Queue. Offer ("hello ");
- Queue. Offer ("world! ");
- Queue. Offer ("Hello! ");
- System. Out. println (queue. Size ());
- String STR;
- While (STR = queue. Poll ())! = NULL ){
- System. Out. Print (STR );
- }
- System. Out. println ();
- System. Out. println (queue. Size ());
- }
- }