java优先级队列如何设想工作? [英] How java priority Queue is suppose to work?

查看:117
本文介绍了java优先级队列如何设想工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短篇小说,我正在执行一个图表,现​​在我在Kruskal上工作,我需要一个优先级队列。我对优先级队列的定义是,具有最小密钥的元素会先来吗?这是错的吗因为当我在队列中插入加权边缘(或数字)时,它们不会被排序。

Short story, I'm implementing a graph and now I'm working on the Kruskal, I need a priority queue. My definition of a priority queue is that the element with the smallest key would come first? Is this wrong? Because when I insert the weighted edges(or numbers) in the queue they don't end up sorted.

PriorityQueue<Integer> tja = new PriorityQueue<Integer>(); 
tja.add(55);
tja.add(99); 
tja.add(1); 
tja.add(102);
tja.add(54);
tja.add(51);
System.out.println(tja);

这将打印出来; [1,54,51,102,99,55]。这不是像我想要的那样排序!是的,我做了一个执行者,进入优先级队列,从边缘对象中提取数字,并根据该int进行比较。所以这应该是有效的,还是我完全误解了这个数据结构如何运作的整个概念?

That would print out this; [1, 54, 51, 102, 99, 55]. This is not sorted like I want them to be! And yes I made a comperator that goes into the priority queue that extracts the number from the edge object and compares based on that int. So this should work, or have I just completely misunderstood the entire concept of how this data structure works?

推荐答案

System.out.println正在调用使用迭代器的toString()方法,不能保证尊重自然的订购。从文档:提供的迭代器方法iterator()不能保证以任何特定顺序遍历优先级队列的元素。

System.out.println is invoking the toString() method, which is using the iterator, which is not guaranteed to respect the natural ordering. From the docs: "The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order."

这篇关于java优先级队列如何设想工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆