All About Queue Interface in Collection in Java



The Queue interface extends to Collection interface and declares the behavior of a queue. It is often first in first out list. In a queue ,elements can only be removed from head of that queue.

The method available in a Queue interface are:

element();//returns the element at the head of the queue.The element is not removed.
offer(Object obj);//attempts to add an element to the queue.Returns true if added,false otherwise.
peek();//returns the element at the head of the queue.The element is not removed.
poll();//returns the element at the head of the queue after removing the current head.
remove();//returns the element at the head of the queue after removing the element.
 
The method element() and peek() are similar. If the queue is empty element() throws NoSuchElementException while peek() returns null.
The method poll() and remove() are also similar. poll() returns null when the queue is empty and remove() throws NoSuchElementException
All About Queue Interface in Collection in Java All About Queue Interface in Collection in Java Reviewed by Animesh Chatterjee on December 19, 2018 Rating: 5

No comments:

Powered by Blogger.