Java Instance-Queue usage

Source: Internet
Author: User

A queue is a special linear table that allows for deletion only on the front end of the table, and on the back end of the table.

The LinkedList class implements the queue interface, so we can use LinkedList as a queue.

The following example demonstrates the use of a queue:

Importjava.util.LinkedList;ImportJava.util.Queue; Public classMain { Public Static voidMain (string[] args) {//The Add () and remove () methods throw exceptions when they fail (not recommended)queue<string> queue =NewLinkedlist<string>(); //adding elementsQueue.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 it in a queue         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); }    }}

Main.java fileImport Java.Util.LinkedList;Import Java.Util.Queue;Public Class Main { Public Static void Main(String[] Args) { //The Add () and remove () methods throw exceptions when they fail (not recommended) Queue<String>Queue=New LinkedList<String>();//adding elements 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 it in a queue 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())// return the first element Span class= "hl-reserved" >for (string q: queue { System< Span class= "Hl-code". out. Println (q) } } }

The above code runs the output as:

ABCDE = = =Poll = ABCDE = = =element=bbcde  = = = Bbcde 

Java Instance-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.