我应该使用什么类型的阻塞队列?

发布于 2024-12-21 13:06:45 字数 178 浏览 1 评论 0原文

我想实现一个阻塞,我可以随时以任何方式添加元素。但我必须能够按顺序访问它们。

例如,考虑一个 x 元素的队列,其中添加了元素 1,4,8,10。所以10可以被访问,但是在添加和访问9之前,8是不能访问的。简而言之,所有要素都是相互关联的。

如果java已经实现了这种类型的集合,请告诉我。所以我可以直接使用它。

I want to implement a blocking where I can add element any time, any way. But I must be able to access them sequentially.

For example, consider a queue of x elements where the elements 1,4,8,10 have been added. So 10 can be accessed, but until 9 is added and accessed, 8 cant be. In short, all the elements are interrelated.

Please let me know if java already has such type of collection implemented. So I can use it directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我入戏太深 2024-12-28 13:06:45

您正在谈论堆栈或队列,具体取决于您是否希望它们成为 FIFOLIFO,并首先按数字顺序插入元素 - 这意味着您'在插入时对元素重新排序它们始终处于正确的数字顺序。

通过在排序时按数字顺序对元素进行排序,可以保证它们在删除它们时按照您期望的顺序排列。

您可以使用 Java 的 LinkedList 在列表中所需的位置插入元素,也可以根据需要从列表的“后面”或“前面”删除它们。

最后,为了确保除非它是序列中的下一个项目,否则您无法删除该项目,您只需在从列表中删除它之前检查该值,以确保它是按顺序的下一个在删除最后一个元素之后。如果未通过该标准,则返回“false”或其他一些值,表明此时无法从列表中删除任何内容。

另外,请查看这个问题:创建阻塞队列在 .NET 中?- 它不是 Java,但非常相似,并且可以提供一些见解。

You're talking about either a stack or a queue, depending on if you want them to be FIFO or LIFO, combined with inserting the elements in numerical order in the first place - meaning you're sorting the elements upon insertion so they are always in the correct numerical order.

By sorting the elements by numerical order upon sort, it's guaranteed that they'll be in the order you expect when you remove them.

You can use Java's a LinkedList to both insert elements where you want them in the list, and also remove them either form the "back" or "front" of the list, as needed.

Finally, in order to ensure that you cannot remove an item unless it is the next in the sequence, you need to simply do a check on the value before removing it from the list to make sure that it was sequentially the next one after the last element removed. If it doesn't pass that criteria, either return "false" or some other value that indicates that nothing can be removed from the list at this time.

Also, check out this question: Creating a blocking Queue<T> in .NET? - it's not Java, but it's very similar, and may provide some insight.

浮光之海 2024-12-28 13:06:45

使用按顺序将项目推送到队列的逻辑包装队列。例如。创建一个实现 Queue 和 BlockingQueue 的类。大多数方法(如 get)直接委托给内部 ArrayBlockingQueue 之类的东西,但“put”会将内容按顺序放入内部 ArrayBlockingQueue 中。如果使用尚不应该访问的元素调用 put,则将其存储在另一个中间数据结构中。

每次使用“下一个”元素调用 put 时,都会将其推送到队列中,然后遍历中间数据结构并将任何元素添加到现在可以添加的队列中。

Wrap Queue with logic that pushes items to it in order. Eg. create a class which implements Queue and BlockingQueue. Most methods (like get) delegates directly to something like an internal ArrayBlockingQueue, but "put" would put stuff onto the internal ArrayBlockingQueue in order. If put is called with an element that should not yet be accessible, you store it in another, intermediate, data structure.

Every time put is called with an element that is "next up", you push it to the queue, and then go through your intermediate data structure and add any elements to the queue that can now be added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文