Initial learning of the Java queue

Source: Internet
Author: User

The stack is a linear table that is advanced, allowing elements to be added at the top of the table and deleted at the top of the table.

How to construct the stack:
Public stack () {}

Push (): Adds an element at the top of the table and AddElement () is the same.

Pop (): Get and remove the first element at the top of a table

Peek (): Gets the first element at the top of a table but does not remove

  

A queue is a special linear table that only allows elements to be added at the back end of the table, deleting elements at the front of the table.

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

Offer (): Adds an element to the back end of the table.

Poll (): Gets and deletes the first element at the front of the table.

Peek (): Gets the first element at the front of the table.

Element (): Gets the first element at the front of the table.

Add (): Inserts the specified element at the tail of this queue. As the queue is unbounded, this method would never throw IllegalStateException or return false .

Offer (): Inserts the specified element in the tail of this queue. As the queue is unbounded, this method would never return false .

Difference: Both are inserting elements into the tail of the queue, different times, when the queue bounds are exceeded, the Add () method throws an exception for you to handle, and the offer () method returns false directly

The Add () and remove () methods throw exceptions when the operation fails, so it is not recommended.

Initial learning of the Java queue

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.