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.
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.
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.