A Queue is a collection for holding elements prior to processing. It provides additional insertion, removal and inspection operations:
- E element()
- boolean offer(E e)
- E peek()
- E poll()
- E remove()
The general-purpose implementation of Queue is LinkedList.
Each Queue method exists in two forms: one throws an exception when fails, and the other returns a special value when fails (null or false, depending on return type).
Methods that will throw an exception: add, element, remove.
Methods that will return a special value: offer, peek, poll.
Queues typically (but not necessarily) order elements in a FIFO manner, which stands for First In – First Out. In this case all new elements are inserted at the tail of the queue. Other kinds of queues may use different insertion rules.
If we need to control elements ordering we can use PriorityQueue that stores the elements in natural order or uses Comparator or Comparable to define the right order.