- PriorityQueue Class is 1 of the collection framework degree which implements Queue, Serializable, Iterable too Collection interfaces.
- It is using natural ordering to lodge thus elements of PriorityQueue.Or elements volition hold out ordered based on Comparator provided at queue construction fourth dimension depending on which constructor is used.
- PriorityQueue is based on a priority heap. Heap is tree based information construction where all the nodes of tree are inward a specific order. Please read almost Heap to larn how it works.
- PriorityQueue does non permit non-comparable objects too it may throw ClassCastException if you lot volition endeavour to create so.
- PriorityQueue does non permit zippo elements every bit well.
- PriorityQueue implementation is non synchronized. So multiple threads should non access it concurrently.
- boolean add(E e) : It volition insert specified chemical constituent inward PriorityQueue.
- void clear() : It volition take away all elements from it to clear PriorityQueue.
- boolean contains(Object o) : It volition banking corporation lucifer too render truthful if specified chemical constituent is available inward PriorityQueue. Else it volition render false.
- boolean offer(E e) : It volition insert specified chemical constituent inward PriorityQueue.
- E peek() : It volition render head(first) of the chemical constituent from queue. If PriorityQueue is empty thus it volition render null.
- E poll() : It volition take away head(first) of the chemical constituent from queue. If PriorityQueue is empty thus it volition render null.
- boolean remove(Object o) : It volition take away specified element's unmarried event from PriorityQueue if it is present.
- int size() : It volition render size of PriorityQueue.
- Object[] toArray() : It volition render elements array from queue.
- <T> T[] toArray(T[] a) : It volition render elements array from queue. The runtime type of the returned array is that of the specified array.
Below given instance volition demo you lot how to operate PriorityQueue methods.
PriorityQueueExample :
package JAVAExamples; import java.util.PriorityQueue; populace degree PriorityQueueExample { populace static void main(String[] args) { //Create PriorityQueue. PriorityQueue q=new PriorityQueue(); //Add items to PriorityQueue. q.add("one"); q.add("two"); q.add("three"); q.add("four"); q.add("five"); q.add("six"); q.add("seven"); //Get size of PriorityQueue. System.out.println("Size of PriorityQueue is : "+q.size()); //Print values of PriorityQueue. System.out.println("PriorityQueue elements are : "+q); //Get caput of PriorityQueue. System.out.println("Head of PriorityQueue is : "+q.peek()); //Remove caput of PriorityQueue. q.poll(); System.out.println("Now PriorityQueue elements are : "+q); } }
Output :
Size of PriorityQueue is : 7 PriorityQueue elements are : [five, four, seven, two, one, three, six] Head of PriorityQueue is : 5 Now PriorityQueue elements are : [four, one, seven, two, six, three]