From a7d729d431aef5f9e7a4846462743b47c3d1b604 Mon Sep 17 00:00:00 2001 From: Ankush1oo8 Date: Sat, 9 Mar 2024 20:14:08 +0530 Subject: [PATCH] using priority queue with objects --- Heap/PQWithObjects.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Heap/PQWithObjects.java b/Heap/PQWithObjects.java index a9628de..47bc6c8 100644 --- a/Heap/PQWithObjects.java +++ b/Heap/PQWithObjects.java @@ -15,7 +15,16 @@ public int compareTo(student s2){ } } public static void main(String[] args) { - PriorityQueue<>pq=new PriorityQueue<>(); + PriorityQueuepq=new PriorityQueue<>(/*to print in reverse order put 'Comparator.reverseOrder()'here */); + pq.add(new student("A", 4)); + pq.add(new student("B", 5)); + pq.add(new student("C", 2)); + pq.add(new student("D", 12)); + + while(!pq.isEmpty()){ + System.out.println(pq.peek().name+" -> "+pq.peek().rank); + pq.remove(); + } } }