Advertisement
class PriorityQueue<E>
1) PriorityQueue() |
---|
This constructor creates an empty PriorityQueue of a particular type. Example - |
PriorityQueue<Integer> arrL= new PriorityQueue<Integer>();
2) PriorityQueue(Collection <? extends E>c) |
---|
This constructor creates a PriorityQueue initialized with the elements of Collection c. With a condition, that, Collection c holds similar type of objects as the type declared by the PriorityQueue. |
PriorityQueue<Integer> arrL1 = new PriorityQueue<Integer>():
PriorityQueue<Integer> arrL2 = new PriorityQueue<Integer>(arrL1);
3) PriorityQueue(Comparator<? super E> comp) |
---|
This constructor will create an empty PriorityQueue which is sorted according to the specified Comparator(comp) in constructor. |
PriorityQueue<String> ts2 = new PriorityQueue<String>(new Comp());
Methods | Description |
---|---|
boolean add(E e) | Inserts the element e at the end of an PriorityQueue. |
void addFirst(E e) | Inserts the element e at the beginning of the PriorityQueue. |
void addLast(E e) | Inserts the element e at the end of PriorityQueue. |
E removeFirst() | Removes the first element of the PriorityQueue. |
E removeLast(E e) | Removes the last element of the PriorityQueue. |
boolean offer(E e) | Inserts the element e in the PriorityQueue. |
E peek() | Returns the head(first) element of the PriorityQueue, without removing it. |
E poll() | Returns and removes the head(first) element of the PriorityQueue. |
int size() | Returns the total number of elements in an ArrayList. |
void clear() | Removes all of the elements from the ArrayList |
E get(int index) | Returns the element at the specified index in an ArrayList. |
E remove(int index) | Removes an element at a specified index in an ArrayList. |
boolean remove(Object o) | Removes the first occurrence of an Object from the ArrayList, if it is present. |
Object[] toArray() | Returns an Object array containing all the elements of an ArrayList. |
Iterator |
Returns an Iterator to iterate over elements of an ArrayList. |
ListIterator |
Returns a ListIterator to iterate over elements of an ArrayList. |
Advertisement
//Java - Creating a PriorityQueue and iterating over its elements
import java.util.*;
public class PriorityQueue1
{
public static void main(String... ar)
{
//Passing an object of implementer of Comparator Interface
PriorityQueue<Integer> priorityQ= new PriorityQueue(new PriorityQueue1());
priorityQ.offer(4);
priorityQ.offer(1);
priorityQ.offer(5); // Offer method adds an object in the PriorityQueue
priorityQ.add(2); // add() also adds an object in the PriorityQueue
priorityQ.add(3);
priorityQ.remove(3); // to remove 3 from the PriorityQueue
//Calling the iterator() method to iterate over the elements of PriorityQueue
Iterator itr = priorityQ.iterator();
System.out.print("Contents of PriorityQueue are : ");
while(itr.hasNext())
{
System.out.print(itr.next() + " " );
}
System.out.println("\nSize of PriorityQueue after using poll() = "+ priorityQ.size());
}
}
Contents of PriorityQueue are : 1 2 5 4
Size of PriorityQueue after using poll() = 4
import java.util.*;
public class PriorityQueue1
{
public static void main(String... ar)
{
PriorityQueue<Integer> priorityQ= new PriorityQueue<Integer>();
priorityQ.offer(4);
priorityQ.offer(1);
priorityQ.offer(5); // Offer method adds an object in the PriorityQueue
priorityQ.add(2); // add() also adds an object in the PriorityQueue
priorityQ.add(3);
priorityQ.remove(3); // to remove 3 from the PriorityQueue
System.out.println("PriorityQueue contents may appear out-of-order : " + priorityQ);
System.out.print("PriorityQueue in an ascending ordering using poll() method : ");
//poll() returns & removes the first element of Queue or returns null, if Queue is empty
for( Integer i= priorityQ.poll(); i!=null; i=priorityQ.poll())
{
System.out.print( i + " ");
}
System.out.println("\nSize of PriorityQueue after using poll() = "+ priorityQ.size());
}
}
PriorityQueue contents may appear out-of-order : [1, 2, 5, 4]
PriorityQueue in an ascending ordering using poll() method : 1 2 4 5
Size of PriorityQueue after using poll() = 0
//Giving descending order to the elements in PriorityQueue by implementing the Comparator Interface
import java.util.*;
class PriorityQSort implements Comparator<Integer>
{
//Overriding Comparator's compare() method to give a descending ordering
public int compare(Integer one, Integer two)
{
return(two-one);
}
public static void main(String... ar)
{
//Passing an object of implementer of Comparator Interface
PriorityQueue<Integer> pq= new PriorityQueue<Integer>(new PriorityQSort());
pq.offer(4);
pq.offer(1);
pq.offer(5); // Offer method adds an object in the PriorityQueue
pq.add(2); // add() also adds an object in the PriorityQueue
pq.add(3);
pq.remove(3); // to remove 3 from the PriorityQueue
System.out.println("Content of PriorityQueue without descending ordering" + pq);
System.out.println("Size of PriorityQueue = "+ pq.size());
System.out.println("Content of PriorityQueue with descending ordering : ");
//poll() returns & removes the first element of Queue or returns null, if Queue is empty
for( Integer i= pq.poll(); i!=null; i=pq.poll())
{
System.out.print( i + " ");
}
}
}
Content of PriorityQueue without descending ordering[5, 2, 4, 1]
Size of PriorityQueue = 4
Content of PriorityQueue with descending ordering : 5 4 2 1
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement