Java priority queue remove example.
Java priority queue remove example.
The PriorityQueue is java collection class. It is used for implementation of priority queue data structure. So it provides different method. The remove is a method of it. It is used for removing a element from this queue.
Code:
PriorityQueueremoveMethodExample.java
import java.util.Iterator; import java.util.PriorityQueue; public class PriorityQueueremoveMethodExample { public static void main(String[] args) { PriorityQueue obQueue = new PriorityQueue(); obQueue.add(1); obQueue.add(2); obQueue.add(4); Iterator obIter = obQueue.iterator(); System.out.println("Before remove method element in Priority Queue."); while (obIter.hasNext()) { System.out.print(obIter.next() + " "); } boolean b = obQueue.remove(1); if (b == true) { System.out.println("\nElement successfully removed."); } else { System.out.println("Element not removed."); } Iterator obIter1 = obQueue.iterator(); System.out.println("After remove method element in Priority Queue."); while (obIter1.hasNext()) { System.out.print(obIter1.next() + " "); } } }Output:
Before remove method element in Priority Queue. 1 2 4 Element successfully removed. After remove method element in Priority Queue. 2 4 |
Download this code.
[ 0 ] Comments