Queue Interface


So far I have covered most of the stuff about Collection and it’s interfaces like List, Set and Map. Queue interface is another important one which was added into Java API during version 1.5.

Queue Interface

Java 5 introduced queue implementations in the Collections framework. Queue implementations share a new Queue interface and provide additional insertion, removal, and inspection operations, for accessing the head and tail of the queue. In a queue, items are always placed at the end or “tail” of the list, and read from the beginning or “head” of the list.

1) PriorityQueue

Priority Queue was introduced in Java 1.5. Unlike FIFO -First In First Out, Priority Queue(as name suggests) creates “priority in – Priority Out” queues. Priority Queues order the elements based on either by natural order or according to Comparator. The ordering of elements represents their relative priority.

Main methods of Priority Queue are :

offer() – To add elements to PriorityQueue

poll() – Returns the highest priority entry AND removed the entry from the queue

peek() – Returns the highest priority entry but does not remove.

offer() and poll() methods cant be used while iterating queue in a loop as they throw concurrentModification exception.

2) LinkedList

In the LinkedList structure, elements are doubly-linked to each other.

LinkedList extends AbstractSequentialList and implements List interface, Cloneable, Serializable. Starting Java 5, it has been enhanced to implement java.util.Queue interface as well and it now supports queue methods like – peek(), poll(), offer().

LinkedList is not synchronized and it also permits all elements including null. In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue.

LinkedList has two constructors:

LinkedList() – Constructs an empty LinkedList.
LinkedList(Collection c) – Constructs a Linked List containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

Previous articleMap Interface
Next articleSkip Test Cases – Maven Build
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.