2

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.

3
  • 1
    Optimization of....what? Better than what? Voting to close. Commented Jan 31, 2012 at 2:48
  • Have you profiled the application to determine if it is worthwhile to even think about optimizing the queues? Commented Jan 31, 2012 at 3:03
  • Use of a Hashtable or a Hashmap will depend on the need of concurrent access. If you need to concurrency using a ConcurrentHashMap would be a better choice IMO. Commented Jan 31, 2012 at 4:22

2 Answers 2

4

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:

  • Add an item to the queue with a given ID
  • Pull the first item from the Queue with a given ID

Which is probably the usage pattern you are going to be needing in most cases.....

Sign up to request clarification or add additional context in comments.

1 Comment

Pretty much explains what I'm looking for, thanks for being patient with me.
0

since java is statically typed, i would definitely use a hashtable... (that is, if we are talking about optimization)

3 Comments

could you please explain a little more on the answer?
well.... in statically typed languages array sizes are defined before an array is used.. so in ur case it would be bad to use an array since you will be forced to have only a constant number of queues in your collection...but if you were using a dynamically typed language like ruby, python or php...you can use an array with out specifying the size...so it would have been more effective to use an array if you were using any of the dynamically typed languages (since you would have a better indexing system i.e 0,1,2...)but since java is not one of them hashtables are your best choices
@bernabasd you can edit your original answer to add the details

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.