Example of priority queue contains method in java.
Example of priority queue contains method in java.
The contains(E a) is a method PriorityQueue class in java. The returns boolean value, if given element is present in priority queue than it returns true otherwise returns false.
Code:
PriorityQueueContainsMethod .java
import java.util.Iterator;
import java.util.PriorityQueue;
public class PriorityQueueContainsMethod {
public static void main(String[] args) {
PriorityQueue obQueue = new PriorityQueue();
obQueue.add(1);
obQueue.add(2);
obQueue.add(3);
obQueue.add(4);
obQueue.add(5);
Iterator obIt = obQueue.iterator();
System.out.println("Element of queue");
while (obIt.hasNext()) {
System.out.print(obIt.next()+" ");
}
boolean b = obQueue.contains(4);
if (b == true) {
System.out.println("\nValue is available in queue.");
} else {
System.out.println("\nValue is not available in queue.");
}
}
}
Output:
|
Element of queue 1 2 3 4 5 Value is available in queue. |

[ 0 ] Comments