Java PriorityQueue带有自定义对象无法正确排序
我不完全了解如何使用Java Priorityqueue(Max Heap)作为自定义对象。
我正在研究 leetcode问题我的代码必须重新订购。句子中的单词按单词长度。我的本能是,我可以使用优先级为我完成单词顺序的工作。为此,我认为我可以用自定义对象跟踪单词:
public class word implements Comparable<word>{
public String theWord;
public int len, order;
public word(String w, int order) {
this.theWord = w;
this.order = order;
this.len = w.length();
}
@Override
public int compareTo(word o) {
return this.len - o.len; // sorting behavior controlled here, right???
}
public String toString() {
return this.theWord+"("+this.order+") "; // for troubleshooting
}
}
然后:
public String arrangeWords(String sentence) {
PriorityQueue<word> maxHeap = new PriorityQueue<>(Comparator.naturalOrder());
String[] words = sentence.split(" ");
for( int i=0; i<words.length; i++ ) {
maxHeap.offer( new word(words[i], i) );
}
}
我要测试的第一个句子是“ leetcode很酷
>”。 (来自LC帖子。)
订购我 希望 for is:“ is cool leetcode
>”(最短的单词顺序)
但是,当我运行上面的代码并检查调试器中的优先级时,我会看到:
is(1) leetcode(0) cool(2)
soooo ...什么呢?我根本不明白这是如何订购的。这不是原始顺序(由括号表示),而不是按字母顺序排列的,甚至不是按字母顺序排列。我不知道PriortityQueue如何决定如何订购Word
对象。我以为类Word
's compareTo()
方法将迫使我想要的订购。 (我已经看到了其他如此帖子。)但事实并非如此。有人看到我出了什么问题吗?谢谢。
I don't completely understand how to use a Java PriorityQueue (max Heap) for custom objects.
I'm working on a LeetCode problem where my code must reorder the words in a sentence by word length. My instinct was that I could use a PriorityQueue to do the work of word-ordering for me. To do that, I thought I could track words with a custom object:
public class word implements Comparable<word>{
public String theWord;
public int len, order;
public word(String w, int order) {
this.theWord = w;
this.order = order;
this.len = w.length();
}
@Override
public int compareTo(word o) {
return this.len - o.len; // sorting behavior controlled here, right???
}
public String toString() {
return this.theWord+"("+this.order+") "; // for troubleshooting
}
}
Then:
public String arrangeWords(String sentence) {
PriorityQueue<word> maxHeap = new PriorityQueue<>(Comparator.naturalOrder());
String[] words = sentence.split(" ");
for( int i=0; i<words.length; i++ ) {
maxHeap.offer( new word(words[i], i) );
}
}
The first sentence I'm using to test is "leetcode is cool
". (From the LC post.)
The ordering I'm hoping for is: "is cool leetcode
" (shortest-to-longest word order)
But when I run the above code and check the PriorityQueue in the debugger, I see:
is(1) leetcode(0) cool(2)
Sooo... what the heck? I don't understand how this is ordered at all. This is not the original order (indicated by parenthesis), not in length order, not even in alphabetical order. I have no idea how the PriorityQueue is deciding how to order the word
objects. I thought that the class word
's compareTo()
method would force the ordering that I want. (I've seen this with other SO posts.) But not so. Does someone see what I'm going wrong? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将它们插入优先队列。但是然后您需要 Poll 排队获得正确的单词顺序的队列。
另外,请注意
订单
字段不会仅仅因为将其插入优先队列而被更改。它只是显示了单词在原始句子中出现的顺序。在循环插入的位置之后,在循环之后写下该循环。然后再次执行。您会看到正确的顺序。
You inserted them in priority queue. But then you need to
poll
the queue to get the right order of words.Also please note the
order
field won't get altered just because you inserted them in priority queue. It just shows the order in which the word appears in original sentence.Write that loop after your
for
loop where you inserted. Then execute again. You will see right order.Priorityqueue(Minheap)坚持认为最低元素的长度最低。其余元素将以随机顺序为单位。一旦您对顶部元素进行了轮询,然后重新排序就会发生(在技术上令人震惊),从而使剩余的最小元素成为最高元素。正如已经指出的那样,您需要对所有对象进行轮询,并使它们成为句子的一部分。
另外,解决问题的另一种方法是 - &gt;您可以简单地使用字符串数组上的比较器。
PriorityQueue( minHeap) maintains that the top element is of the lowest length . The remaining elements will be in random order. Once you poll the top element, then re-ordering happens( upHeapify -technically ) that makes the smallest from the remaining to become the top element. As already pointed out, you need to poll all the objects and make them part of your sentence.
Also, another way to go about the problem was -> you could have simply used the comparator on the array of String.