Online Java Compiler By
JavaTpoint.com
import java.util.PriorityQueue; class Student extends Object implements Comparable< Student>{ String rollNo, name,course; int age; public Student(String rollNo, String name, String course,int age) { this.rollNo= rollNo; this.name = name; this.course= course; this.age= age; } @Override public int compareTo(Student o) { return 0; } } public class JavaPriorityQueueclearMethodExample2 { static int i =1; public static void main(String[] args) { PriorityQueue
pQueue = new PriorityQueue<>(); Student val1 = new Student("2","ravi","C",21); Student val2 = new Student("1","Geetanjali","Java",21); Student val3 = new Student("3","Himanshu Bhardwaj","C++",21); pQueue.add(val1); pQueue.add(val2); pQueue.add(val3); for(Student xyz:pQueue){ System.out.println(i++ +" Roll No = "+xyz.rollNo+"\n Name = "+xyz.name +"\n Course = " +xyz.course+"\n Age = "+xyz.age+"\n"); } pQueue.clear(); System.out.println("After clearing the queue :\n "+pQueue); } }
Output