Queues in Java

Source: Internet
Author: User

About queues

A queue is a link-based, thread-free, secure queue that uses FIFO rules to sort the nodes, and when we add an element, it is added to the end of the queue, and when we get an element, it returns the element of the queue header.

For example, the queue can be likened to a line of tickets to a team of people, first-come to the first person to receive the ticket, after the person to pick up the ticket.

The queue can operate on both the head and tail, if I define an array, I can take the a[o], or add a new element after A[5] a[6].

About the Queue interface

Summary of methods for queues

Modifiers and types Methods and instructions
Boolean add(E e)
Inserts the specified element into this queue (if it is feasible immediately and does not violate the capacity limit), returns trueon success, and throws illegalstateexceptionif no space is currently available.
E element()
Gets, but does not remove the header of this queue.
Boolean offer(E e)
Inserting the specified element into this queue (if it is feasible immediately and without violating capacity limits) is generally preferable when using a capacity-constrained queue, add(E) which may not be able to insert elements, but only throws an exception.
E peek()
Gets the header of this queue without removing it, or nullif this queue is empty.
E poll()
Gets and removes the header of this queue, and returns nullif this queue is empty.
E remove()
Gets and removes the header for this queue.

Instance

Here I'm going to use arrays to implement stacks

PS: I use Java generics here, the generics about Java in the previous essays I have introduced.

1 Importjava.util.Arrays;2 3  Public classQueue<e> {4 5     //create a new array of length 06     Privateobject[] src =NewObject[0];7 8     //joining data to a queue9      Public voidAdd (E s) {Ten         //copy src array, length is the length of the original array +1 OneE[] e = (e[]) arrays.copyof (SRC, src.length + 1); A         //Passes the incoming data to the last element of the new array -E[E.LENGTH-1] =(E) s; -src =e; the     } -  -     //view data for a queue header -      PublicE Peek () { +         //determine if the queue is empty -         if(src.length==0) { +             return NULL; A         } at         //returns the first element of a src array -         return(E) src[0]; -     } -  -     //Eject Queue header data [remove] -      PublicE Poll () { in         if(Src.length = = 0) { -             return NULL; to         } +         //copies the SRC array to the new array for easy data ejection from the queue header -E[] s =(e[]) arrays.copyof (SRC, src.length); the         //create a new array of length 1 for the original array, and then copy the original array to the new array after removing the first element *object[] St =NewObject[src.length-1]; $          for(inti = 0; i < st.length; i++) {Panax NotoginsengSt[i] = (E) Src[i + 1]; -         } thesrc =St; +         returnS[0]; A     } the  +     //get the length of the queue -      Public intsize () { $         returnsrc.length; $     } -  -     //determine if the queue is empty the      Public Booleanempty () { -         returnSrc.length = = 0;Wuyi     } the      -      Public Static voidMain (string[] args) { WuQueue<string> q=NewQueue<string>(); -          About         //joining data to a queue $Q.add ("AA"); -Q.add ("BB"); -Q.add ("CC"); -Q.add ("DD"); A          +SYSTEM.OUT.PRINTLN ("The data for the queue header is:" +Q.peek ()); the          -System.out.println ("Queue Length is:" +q.size ()); $          the          while(!Q.empty ()) { theSystem.out.println ("Popup Data:" +Q.poll ()); the         } the     } -}

Operation Result:

The data for the queue header is: AA Queue Length: 4 popup data: aa popup data: BB popup data: CC popup data: DD

Results analysis

Follow the queue ' FIFO ' feature, enter AA, BB, CC, DD, eject AA, BB, CC, DD.

According to this feature, the queue is necessary at some point.

Queues in Java

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.