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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative