Program to demonstrate "Queue" class in Java's Collection Framework
The Queue interface order elements in a FIFO (first-in-first-out) manner. The PriorityQueue class has following methods:
PROGRAM
- boolean add(E e) : Inserts the specified element into the priority queue.
- void clear() : Removes all of the elements from the priority queue.
- boolean contains(Object o) : Returns true if the queue contains the specified element.
- boolean remove(Object o) : Removes a single instance of the specified element from the queue, if it is present.
- int size() : Returns the number of elements in the collection.
PROGRAM
import java.util.PriorityQueue;
import java.util.Queue;
class QueueDemo {
public static void main(String[] args) {
Queue<String> p=new PriorityQueue<String>();
p.add("Apple");
p.add("PineApple");
p.add("Orange");
p.add("Banana");
p.add("Raddish");
System.out.println("The Elements Are");
for(String q:p)
System.out.println(q);
}
}
OUTPUT
C:\>javac QueueDemo.java C:\>java QueueDemo The Elements Are Apple Banana Orange PineApple Raddish
Comments
Post a Comment