The Java.util.PriorityQueue.iterator() method is used to return an iterator of the same elements as the Priority Queue. The elements are returned in random order from what present in the Queue.
Syntax:
Iterator iterate_value = Priority_Queue.iterator();
Parameters: The function does not take any parameter.
Return Value: The method iterates over the elements of the queue and returns the values(iterators).
Below programs illustrate the Java.util.PriorityQueue.iterator() method:
Program 1:
Output:
PriorityQueue: [4, Geeks, To, Welcome, Geeks]
The iterator values are:
4
Geeks
To
Welcome
Geeks
Program 2: