Java PriorityQueue Class

  • PriorityQueue Class is i of the collection framework cast which implements Queue, Serializable, Iterable together with Collection interfaces.
  • It is using natural ordering to guild together with therefore elements of PriorityQueue.Or elements volition last 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 close Heap to acquire how it works.
  • PriorityQueue does non permit non-comparable objects together with it may throw ClassCastException if you lot volition endeavor to create so.
  • PriorityQueue does non permit naught elements every bit well.
  • PriorityQueue implementation is non synchronized. So multiple threads should non access it concurrently.
Java PriorityQueue Class

Few methods which are used amongst PriorityQueue are every bit below.
  • boolean add(E e) : It volition insert specified chemical ingredient inward PriorityQueue.
  • void clear() : It volition take all elements from it to clear PriorityQueue.
  • boolean contains(Object o) : It volition cheque together with render truthful if specified chemical ingredient is available inward PriorityQueue. Else it volition render false.
  • boolean offer(E e) : It volition insert specified chemical ingredient inward PriorityQueue.
  • E peek() : It volition render head(first) of the chemical ingredient from queue. If PriorityQueue is empty together with therefore it volition render null.
  • E poll() : It volition take head(first) of the chemical ingredient from queue. If PriorityQueue is empty together with therefore it volition render null.
  • boolean remove(Object o) : It volition take 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 illustration volition demonstrate you lot how to purpose PriorityQueue methods.

PriorityQueueExample :
package JAVAExamples;  import java.util.PriorityQueue;  world cast PriorityQueueExample {   world 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]


More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post