Iterating through PriorityQueue doesn't yield ordered results

后端 未结 3 1109
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 00:56
import java.util.*;
class Priority{  
public static void main(String args[]){  

PriorityQueue queue=new PriorityQueue();  
queue.add(\"Amit\         


        
3条回答
  •  没有蜡笔的小新
    2021-01-25 01:33

    PriorityQueue doesn't store the elements in sorted order, but it allows you to get the elements from it in sorted order. It just makes sure that the the element at the head is the least element as per the ordering used for it.

    So, if you store multiple numbers - 2, 1, 4, 3, 6, 8, it will make sure that 1 is the next element you remove. Then when you remove 1, it moves 2 to head. It doesn't bother about the ordering of rest of the elements.

提交回复
热议问题