为什么Java中的PriorityBlockingQueue没有正确排序? [英] Why does my PriorityBlockingQueue in Java not sort properly?

查看:159
本文介绍了为什么Java中的PriorityBlockingQueue没有正确排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当我添加到优先级队列时,它不会完全按字母顺序排序我的字符串,我看不出原因。

For some reason when I add to the priority queue, it doesn't sort my strings entirely alphabetically and I can't see why.

这是代码添加到PriorityBlockingQueue:

This is the code which adds to the PriorityBlockingQueue:

String toAdd = String.format("%s/%s", directory, s);
outputData.add(toAdd);

但我没有完全排序输出(只有前几行,但你可以看到它没有排序):

But I get not entirely sorted output (only first few lines but you can see it's not sorted):

../StartingTree/files/abknl/apfmpohgyh/a.class
../StartingTree/files/abknl/apfmpohgyh/a.java
../StartingTree/files/abknl/aqybc/aeph.java
../StartingTree/files/abknl/apfmpohgyh/bnjuxxdi.class
../StartingTree/files/abknl/bbxudleuf/jlffhq/y/xwjj/dyetqhsch/bpg.class
../StartingTree/files/abknl/bbxudleuf/mxb/fe/ndmg/axapxuco.html
../StartingTree/files/abknl/aqybc/atyuojdu.txt

这是排序的真实(第一部分)期望输出文件的输出:

And this is the real (first part) of sorted output from the expected-output file:

../StartingTree/files/abknl/apfmpohgyh/a.class
../StartingTree/files/abknl/apfmpohgyh/a.java
../StartingTree/files/abknl/apfmpohgyh/bnjuxxdi.class
../StartingTree/files/abknl/apfmpohgyh/bnjuxxdi.java
../StartingTree/files/abknl/apfmpohgyh/bsqsq.class
../StartingTree/files/abknl/apfmpohgyh/bsqsq.java
../StartingTree/files/abknl/apfmpohgyh/ds.class
../StartingTree/files/abknl/apfmpohgyh/ds.java


推荐答案

我怀疑你是在尝试迭代 PriorityBlockingQueue 并打印元素。

I suspect you are trying to iterate the PriorityBlockingQueue and print the elements.

请注意优先级队列数据结构(AKA )不保证排序 - 它保证头部最小,但在以下任何节点上都没有订单保证。

Note that a Priority Queue data structure (AKA heap) does not guarantee ordering - it guarantees that the head is minimal, but no order guarantee on any of the following nodes.

如果您希望数据保持排序 - 我建议使用类似 ConcurrentSkipListSet (注意它是一个集合 - 因此它不允许重复主题),或维护一个已排序的< a href =http://docs.oracle.com/javase/6/docs/api/java/util/List.html =n oreferrer> 列表

If you want your data maintained sorted - I suggest using something like ConcurrentSkipListSet (Note however it is a set - thus it does not allow duplicate entrees), or maintaining a sorted List.

如果您想使用<$ c获取已排序的元素$ c> PriorityBlockingQueue - 您应该迭代删除头并输出新头 - 直到优先级队列耗尽。它将保证有序输出。

If you want to get the sorted elements using a PriorityBlockingQueue - you should iteratively delete the head and output the new head - until the priority queue is exhausted. It will guarantee an ordered output.

这篇关于为什么Java中的PriorityBlockingQueue没有正确排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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