• Post Reply Bookmark Topic Watch Topic
  • New Topic

Queue: how to read the last item  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi everybody,

this is a nice website where to post, there is always someone helpful or at least kind.

I am a beginner in java and I have a system where I have a Worker, which has some input and output.
The inputs and outputs are put in two queues, independently(let's say queueA for the inout and queueB for the output.

Now, from time to time, the state of this Worker is saved, also the content of this two queues is saved in an archive (archiveOut),
in this way(pseudo code):

archiveOut = new Stack<Queue<output>(); // this is the archive

archiveOut.push(worker.output); //when I save the content of the queueB (the output of the worker)

My problem is the following: let's say ofr example that my queueB I want to save is like this: A-B-C-D
where A is the head of the queue. Is there a way to read the last item, which is D in the example?

As far as I read, there is no command to do so, and that's a big problem for me.

Thank you for your attention and for your replies!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't sound like a Queue, which is described here. The idea of a queue is to read only the first item. If you want the last item you can use something like a Stack. Or a double-ended Queue.

Now it just so happens there is a combined stack/queue implementation which might do exactly what you want. You won't find it here, but try here.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor points out the interface documentation is more helpful than the implementation I posted earlier.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!