Use Queue in Java: Java. util. Queue

Source: Internet
Author: User

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

  1. Example:
  2. /**
  3. *
  4. * @ Author Zang XT
  5. */
  6. Import java. util. Queue;
  7. Import java. util. Collections list;
  8. Public class testqueue {
  9. Public static void main (string [] ARGs ){
  10. Queue <string> queue = new queue list <string> ();
  11. Queue. Offer ("hello ");
  12. Queue. Offer ("world! ");
  13. Queue. Offer ("Hello! ");
  14. System. Out. println (queue. Size ());
  15. String STR;
  16. While (STR = queue. Poll ())! = NULL ){
  17. System. Out. Print (STR );
  18. }
  19. System. Out. println ();
  20. System. Out. println (queue. Size ());
  21. }
  22. }
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.