In this tutorial, we will learn about Queue Interface and it's methods in Java.
Java Collection frameworks provide Queue Interface which works on Queue data structure.It is present in java.util package and it implements Collection Interface.It works on the principle of First-In-First-Out(FIFO).
The following are the characteristics of the queue:
We can not create objects of an interface so we need derived-classes to create objects.So Java provided us 3 derived classes for our use.They are :
Some of the commonly used methods of the Queue interface are:
Methods | Description |
---|---|
add(): | It inserts the given element into the queue and return true. |
offer(): | It inserts the given element into the queue and return true. |
element(): | It returns the head of the queue. It throws an exception if the queue is empty. |
peek(): | It returns the head of the queue and returns null if the queue is empty. |
remove(): | It returns and removes the head of the queue.It throws exception if found empty. |
poll(): | It returns and removes the head of the queue. Returns null if the queue is empty. |