Collection framework - Queue interface and its implementation classes

Queue is based on the underlying data structure queue which has FIFO pattern.

Queue is used when we want to store the elements in a certain order before processing.

Queue interface comes with the following methods :

boolean offer(Object obj) - Attempts to add object to queue. If success, return true else false.
Object peek() - returns the head element. null if queue empty.
Object poll() - returns and removes the head element. returns null if queue empty.
Object remove() - returns and removes the head element. throws exception if queue empty.
Object element() - returns the head element. throws exception if queue empty.

PriorityQueue is the implementation class of the Queue interface.
It stores the elements based on certain sorting order. either natural sorting order or based on specified comparator based sorting order.

PriorityQueue comes with the following constructors :

PriorityQueue() - creates a queue with initial capacity of 11.
PriorityQueue(int initialCapacity) - creates a queue with specified initial capacity.
PriorityQueue(Comparator comp) - creates a queue with specified comparator
PriorityQueue(int initialCapacity, Comparator comp) - creates queue with given capacity & sorting
PriorityQueue(Collection coll) - creates queue from given collection
PriorityQueue(PriorityQueue queue) - creates queue from given queue
PriorityQueue(SortedSet set) - creates queue from given sorted set and applies it sorting order

The example for PriorityQueue can be found here

Comments

Popular posts from this blog

Collection Framework - HashSet And LinkedHashSet class

Collection Framework - Cursors in Java

Hashed data structures