队列集合的 Java 数据结构
如果我有很多队列并且每个队列都有一个唯一的 ID,那么队列哈希表会是最佳选择吗?我知道我问这个问题听起来很奇怪,但只是想知道是否有更好的优化方法。
抱歉缺少信息。我基本上存储由客户端 ID 标识的消息队列。 客户端将请求从服务器获取消息。 在确认未到达服务器的情况下,消息仍保留在队列中,直到客户端再次尝试获取最旧的消息。 这个想法是在客户端未能确认时保留所有消息,并以 FIFO 方式检索所有消息。
If I have many queues and each has an unique ID, would a Hashtable of Queues be the way to go? I know it sounds strange that I'm asking this, but just wondering if there might be a better way for optimization.
Sorry for the lack of information. I'm basically storing queues of messages which are identified by the client id.
The client will request to get messages from the server.
In the case when the ack does not reach the server, the message still remains in the queue until the client makes another attempt to get the oldest message.
The idea is to retain all the messages if the client fails to ack and to retrieve all messages in a FIFO manner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该问题没有提供有关您想对此做什么的任何详细信息。这非常重要,因为使用模式对于确定哪种数据结构对于您的用例来说最有效至关重要。
但我想说,在没有其他细节的情况下,队列的哈希表听起来是一个明智的选择,哈希表使用 ID 作为键,使用相应的队列作为值。
那么以下操作都是 O(1),开销非常低:
这可能是您将需要的使用模式大多数情况......
The question doesn't provide any detail on what you want to do with this. And this is very important, because the usage pattern is critical in determining which data structure is going to be most efficient for your use case.
But I'd say that in the absence of other details, a HashTable of Queues sounds like a sensible choice, with the HashTable using the ID as a key and the corresponding Queue as a value.
Then the following operations will both be O(1) with very low overhead:
Which is probably the usage pattern you are going to be needing in most cases.....
由于java是静态类型的,我肯定会使用哈希表...(也就是说,如果我们谈论优化)
since java is statically typed, i would definitely use a hashtable... (that is, if we are talking about optimization)