Question
Consider the following Java code snippet:
import java.util.PriorityQueue; import java.util.Comparator; class Item { int priority; String name; public Item(int priority, String name) { this.priority = priority; this.name = name; } @Override public String toString() { return name + "(" + priority + ")"; } } public class HeapQuestion7 { public static void main(String[] args) { // Create a min-heap based on 'priority' PriorityQueue pq = new PriorityQueue(Comparator.comparingInt(item -> item.priority)); pq.add(new Item(5, "Task A")); pq.add(new Item(1, "Task B")); pq.add(new Item(10, "Task C")); pq.add(new Item(3, "Task D")); System.out.println(pq.poll().name); System.out.println(pq.poll().name); } } What will be the output of this program?Solution
The PriorityQueue is initialized with a Comparator that orders Item objects based on their priority field in ascending order (min-heap behavior for priority). 1. pq.add(new Item(5, "Task A")); 2. pq.add(new Item(1, "Task B")); 3. pq.add(new Item(10, "Task C")); 4. pq.add(new Item(3, "Task D")); After these additions, the item with the lowest priority value will be at the head of the queue. The priorities are 1, 3, 5, 10. 5. System.out.println(pq.poll().name); The item with priority 1 ("Task B") is removed and its name is printed. 6. System.out.println(pq.poll().name); The next item with the lowest priority (from the remaining items) is the one with priority 3 ("Task D"). It is removed and its name is printed. Therefore, the output is Task B, Task D.
जब वाक्य लिखते समय कोई ऐसा पद जो उस वाक्य में आना आवश्यक है,...
निम्नलिखित कथनों पर विचार कीजिए:
1. वर्ष 1965 में शिक्षा मं�...
कर्तृवाच्य किसे कहते है
जिस समास में पूर्व-पद गौण तथा उत्तर-पद प्रधान हो , उसे कौन स�...
निम्नलिखित प्रश्न में , चार विकल्पों में से , उस विकल्प का �...
इनमें से अघोष वर्ण है :
निम्नलिखित में से सरल वाक्य का चयन कीजिए-
आँखें चुराना इस मुहावरे का अर्थ क्या है?
'उसके द्वारा अपने भाई को पढ़ाया गया। इस वाक्य में वाच्य का �...
सूची – I को सूची – II से सुमेलित कीजिए और सूचियों के नीचे दिए �...