Last Updated : 06 Dec, 2021
It is a priority queue based on priority heap.
public class PriorityQueue extends AbstractQueue implements SerializableMethods:
public boolean add(E e) Parameters : element : the element we need to add. Return : call return true. Exception : -> ClassCastException -> NullPointerException
public Comparator comparator() Parameters : ------- Return : orders the queue or return null, if it is naturally ordered Exception : ----------
public boolean contains(Object obj) Parameters : obj : object to be checked Return : true - if the object is present else, return false Exception :
public Iterator iterator() Parameters : ------- Return : calls iterator over the elements in the queue. Exception : --------
public boolean offer(E element) Parameters : element : specific element to be entered. Return : call return true. Exception : -> ClassCastException -> NullPointerException
public E peek() Parameters : ------- Return : calls if head exists, else null Exception : ------
public E poll() Parameters : --- Return : calls if head exists, else null Exception : ------
public boolean remove(Object obj) Parameters : obj : object to be removed Return : true - if obj is removed Exception : ------
public int size() Parameters : ---- Return : no. of elements Exception : ---------
public Object[] toArray() Parameters : ------ Return : returns an array containing all the elements of PriorityQueue. Exception : --------
public T[] toArray(T[] array) Parameters : array : the array to which are to be sorted. Return : call an array containing all the elements of the array. Exception : -> ArrayStoreException -> NullPointerException
public void clear() Parameters : --- Return : ------ Exception : ------
// Java Program illustrating the methods
// of java.utl.priorityQueue class
// add(), comparator(), conatins(), iterator(), offer()
// peek(), poll(), toArray(), size(), toArray(t[] g1),
// remove(), clear()
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
// Creating a Priority Queue :
PriorityQueue <Integer> geek = new PriorityQueue <Integer> ();
for(int i=2; i<=20; i=i+2)
{
// Use of add() :
geek.add(new Integer (i));
}
System.out.println("geek PriorityQueue : " + geek);
// Use of comparator()
// No ordering is required here as it is naturally ordered.
Comparator geek_comp = geek.comparator();
System.out.println("geek PriorityQueue : " + geek_comp);
// Use of contains()
boolean check = geek.contains(6);
System.out.println("Use of contains() : " + check);
// Use of iterator()
Iterator g_iterator = geek.iterator();
System.out.print("Iterator values : ");
while(g_iterator.hasNext())
{
System.out.print(g_iterator.next() + " ");
}
System.out.println("");
// Use of offer()
geek.offer(3050);
System.out.println("geek PriorityQueue : " + geek);
// Use of peek()
System.out.println("Head of PriorityQueue via peek : " + geek.peek());
//Use of poll()
int h = geek.poll();
System.out.println("\nHead of PriorityQueue via poll : " + h);
System.out.println("geek PriorityQueue bcz of poll() : " + geek);
// Use of remove()
boolean r = geek.remove(8);
System.out.println("\nCan remove : " + r);
System.out.println("geek PriorityQueue bcz of remove() : " + geek);
// use of size()
System.out.println("\nSize of PriorityQueue : " + geek.size());
// Use of toArray()
Object[] g = geek.toArray();
System.out.print ( "Array from PriorityQueue : ");
for ( int i = 0; i<g.length; i++ )
{
System.out.print (g[i].toString() + " ") ;
}
System.out.println("\n");
// Use of toArray(t[] g1) :
Integer[] g2 = new Integer[5];
Integer[] g1 = geek.toArray(g2);
System.out.print ( "Array from PriorityQueue of size 5 : ");
for ( int i = 0; i<g1.length; i++ )
{
System.out.print (g1[i].toString() + " ") ;
}
System.out.println("\n");
// Use of clear()
geek.clear();
System.out.println("PriorityQueue after clear() : " + geek);
}
}
Output :
geek PriorityQueue : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] geek PriorityQueue : null Use of contains() : true Iterator values : 2 4 6 8 10 12 14 16 18 20 geek PriorityQueue : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3050] Head of PriorityQueue via peek : 2 Head of PriorityQueue via poll : 2 geek PriorityQueue bcz of poll() : [4, 8, 6, 16, 10, 12, 14, 3050, 18, 20] Can remove : true geek PriorityQueue bcz of remove() : [4, 10, 6, 16, 20, 12, 14, 3050, 18] Size of PriorityQueue : 9 Array from PriorityQueue : 4 10 6 16 20 12 14 3050 18 Array from PriorityQueue of size 5 : 4 10 6 16 20 12 14 3050 18 PriorityQueue after clear() : []
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4