1 introduction

This article focuses on the Queue interface and LinkedList, one of its three commonly used implementation classes. There are several other common implementation classes, but they are all under the java.util.concurrent package. We will write a series of them later when we talk about multithreading.

Taking a look at the common methods declared in the Queue interface, the basic usage of data really depends on add, delete, change and query.

1.1 Common Methods

Add:

  • offer[XXX](E e)
  • add[xxx](E e)
  • push(E e)

Delete:

  • remove[XXX]()
  • poll[XXX]()
  • pop()

Query:

  • peek[XXX+]()

2 LinkedList

As mentioned in the previous article in this series, LinkedList implements two List and Queue interfaces. See the structure of LinkedList and the structure of nodes in the previous article.

2.1 increase

  1. offer[xxx]

  2. add[xxx]

  3. push

    It can be found that the last essence is to call linkFirst or linkLast. Since the implementation of the two is nearly the same, we only take linkFirst as an example

2.2 delete

  1. remove[XXX]

  1. poll[XXX]()

  2. pop()

    Unlinkxxx (first); unlinkFirst (last); unlinkFirst (first);

2.3 check the

You can see that Peek looks at either the header element or the tail element, so just return first or last.