Wednesday, 30 November 2016

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:
  1. boolean add(E e) : Inserts the specified element into the priority queue.
  2. void clear() : Removes all of the elements from the priority queue.
  3. boolean contains(Object o) : Returns true if the queue contains the specified element.
  4. boolean remove(Object o) : Removes a single instance of the specified element from the queue, if it is present.
  5. 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

No comments:

Post a Comment