aspose file tools*
The moose likes Beginning Java and the fly likes Modify the first element in a queue? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Modify the first element in a queue?" Watch "Modify the first element in a queue?" New topic
Author

Modify the first element in a queue?

Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 98
So how can I modify the first element in a queue?

I am working on an assignment where I may or may not add to the queue each time the loop runs. If something is added, it randomly is assigned a number between one and five.

Once something is added, I need to then update the top object each time the loop runs by decrementing by one. Once the object become zero, I will then remove that object.

I pretty much have it all figured out except how to modify/update the top object.

Any advice is appreciated.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3949
    
  17

The peek() method should give you access to the top, without removing it. That should let you modify it in place.


Steve
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 98
Steve Luke wrote:The peek() method should give you access to the top, without removing it. That should let you modify it in place.


That will show me what the top element is but how do I modify it with that? If I do num.peek()-- or -1 that does not work.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
What, exactly, do you mean by "modify it", then? Once you have a reference to the top element, you can cast it to the correct class (if it's not a genericized collection to begin with) and invoke methods on it. Is that not what you want to do?


Ping & DNS - updated with new look and Ping home screen widget
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 98
Ulf Dittmer wrote:What, exactly, do you mean by "modify it", then? Once you have a reference to the top element, you can cast it to the correct class (if it's not a genericized collection to begin with) and invoke methods on it. Is that not what you want to do?


I suppose I used the incorrect terminology. I just need to be able to decrease its value by one.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

What type of object is this thst you are putting on the Queue? If it's an immutable object like an Integer or Long, then you can't do it. You have to use an object that has a method that decrements the value it holds.


Junilu - [How to Ask Questions] [How to Answer Questions]
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 98
Junilu Lacar wrote:What type of object is this thst you are putting on the Queue? If it's an immutable object like an Integer or Long, then you can't do it. You have to use an object that has a method that decrements the value it holds.


it is an integer
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Then you can't.

You could pop it off, create a new Integer with a value one less, then push it back on...which may or may not solve your problem (and has the potential to cause new ones...)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 98
fred rosenberger wrote:Then you can't.

You could pop it off, create a new Integer with a value one less, then push it back on...which may or may not solve your problem (and has the potential to cause new ones...)


Could i create a method in the class I use to get the service time to decrease the first one?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Then you have to use something else. Ideally, it would allow you to write:
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

fred rosenberger wrote:You could pop it off, create a new Integer with a value one less, then push it back on...which may or may not solve your problem (and has the potential to cause new ones...)


That's an option if it were a Stack, a LIFO data structure. A Queue is a FIFO data structure so you can't push/pop things to/from it.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Raymond Gillespie wrote:Could i create a method in the class I use to get the service time to decrease the first one?


Well, you could have a variable that you update - initialize it to the value of the head element, then decrement it to zero and re-initialize to the value of the new head element. That's a bit messy but that's your choice as the designer. Personally, I would go with the object that has a decrement() method, it's much cleaner.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3949
    
  17

Junilu Lacar wrote:
fred rosenberger wrote:You could pop it off, create a new Integer with a value one less, then push it back on...which may or may not solve your problem (and has the potential to cause new ones...)


That's an option if it were a Stack, a LIFO data structure. A Queue is a FIFO data structure so you can't push/pop things to/from it.


And if you have a Deque you can treat it like a Queue most of the time, except when you need to do this remove/change/add, then treat it like a Stack. There could be issues depending on who accesses it and when...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Raymond Gillespie wrote:Could i create a method in the class I use to get the service time to decrease the first one?

It seems to me that you're overthinking this by a mile.

Back up and explain again what you're trying to do, because your original explanation still mystifies me - Why do you want to keep a counter at the top of the stack/queue? It smells to me of implementation before design.

BTW, as many others have said, and whatever you decide, use a mutable object as your counter - like AtomicInteger.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 98
Winston Gutkowski wrote:
Raymond Gillespie wrote:Could i create a method in the class I use to get the service time to decrease the first one?

It seems to me that you're overthinking this by a mile.

Back up and explain again what you're trying to do, because your original explanation still mystifies me - Why do you want to keep a counter at the top of the stack/queue? It smells to me of implementation before design.

BTW, as many others have said, and whatever you decide, use a mutable object as your counter - like AtomicInteger.

Winston


I am basically simulating customers in line. Each time the loop runs, there is a chance a new customer will come. If a new customer comes, they will be added to the line(queue) and assigned a number between 1 and 5(with random class) and this will be the "time" it takes them to be serviced when they get to the front of the line. So, once a customer reaches the front of the line(first element in queue), each time the loop runs, their waiting time will decrease by one. Once their time has reached 0, they will then be removed from the line.

All I need to know how to do is decrement the first element in a queue. Let's say I use queue.peek() and the integer value is 4. I need to be able to decrease the value of 4 each time the loop runs and then remove that element when it reaches zero. I know how to remove it. I just don't know how to change it to a different value.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Raymond Gillespie wrote:All I need to know how to do is decrement the first element in a queue. Let's say I use queue.peek() and the integer value is 4. I need to be able to decrease the value of 4 each time the loop runs and then remove that element when it reaches zero. I know how to remove it. I just don't know how to change it to a different value.


If you go through all the replies, you can cobble together the answer. Winston already gave you the class you need to use instead of Integer.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

it seems to me your model is wrong. Customers in a queue don't know how long they have to wait. They just wait until a worker calls them.

If you ask me, you should have some kind of timer. so you pick a random value from 1-5. This represents how long it will take to service the current customer. Once the worker is done (i.e. the counter gets to 0), it pops the next customer off the queue.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Fred makes a good point. I assume this for an assignment so is the "decrement the number at the head of the queue" a specified requirement or is it just something you came up with as part of your solution?

A counterpoint though: an OO design doesn't necessarily have to model the real world to a T. You could use the timer concept as described by Fred or you could still go with the "assign a random number" approach but not interpret it as "the time the customer waits" but rather something else such as "the number of items the customer brings to be processed." Like say number of deposits to make or the number of cars to register. This will still correlate to the time they spend at the front of the queue. Removing an element from the front of the queue can simply mean that processing for that element is done. The point is that responsibilities can be assigned in any number of ways and it's just a matter of aligning these assignments with your own mental model so that you can better understand and visualize the problem and create a well-organized solution.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 98
fred rosenberger wrote:it seems to me your model is wrong. Customers in a queue don't know how long they have to wait. They just wait until a worker calls them.

If you ask me, you should have some kind of timer. so you pick a random value from 1-5. This represents how long it will take to service the current customer. Once the worker is done (i.e. the counter gets to 0), it pops the next customer off the queue.


That is what I am doing. Below is my instructions.

How many minutes it takes to process the customer. This should initialized by generating a random number between 1-5. (Use Random class).

The program should simulate 60 minutes of activity at the store. Each iteration of your program should represent one minute. At each iteration (minute), your program should do the following:

Check to see if new customers are added to the queue. There is a 25% chance that new customers show up (need to be added to the queue) every minute.

Update the customer object currently being serviced (if one exists). This will be the customer object at the front of the queue. If the customer has been completely serviced, remove them from the queue
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

If you have to do it that way because the instructions say so, then so be it. But that isn't the way I would do it. I would just take the first customer in the queue and start servicing that customer. I wouldn't leave that customer in the queue while servicing it, though.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Raymond Gillespie wrote:That is what I am doing. Below is my instructions.

OK, now we're getting somewhere: You have a Cashier (or Server of some kind), a Queue, and Customers. The time someone takes to get serviced is determined when they reach the Cashier, not by the fact that they're at the front of the Queue.

In fact, if they're being serviced, they're NOT in the Queue surely? If at some point during your sixty minutes a Customer walks in when there is no Queue (or it's size() is 0) then wouldn't they just walk straight to the Cashier?

It seems to me that your "busy" counter should be based on the Cashier, not on who's at the front of the Queue; and that would hold true whether you have 1 Cashier or 10.

Winston
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

Actually, I wouldn't use a Queue to model a real Queue to begin with. I know it's shocking, but occasionally, people have been known to leave a queue from between the queue.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Paul and Winston: I won't argue that the approaches you would take are valid in the real world but I don't see how it makes much of a difference in a program. As I pointed out before (and they are not my own words), an OO design does not necessarily have to mimic the real world to a T.

So what if you leave the element that you are processing on the queue while you are working with it? What's wrong with waiting until you're done with processing the head element before removing it from the queue? You're only dealing with object references after all. It's not like the object will get mad that they're still in the queue while they're being serviced, right? And why complicate things by adding a concept of a Cashier if there was no requirement to have a Cashier? If this were a program to simulate the movement in a queue based on certain rules, I think the way the problem is stated is good enough to achieve a reasonable approximation.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

The problem is flexibility. It will be tough to model multiple Cashiers with this design..

You can do it by building some sort of work window that is always at the head of the queue, and whose size is equal to number of cashiers. However, that's a lot more complicated than just removing the customer from the queue and giving it to the cashier.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Jayesh A Lalwani wrote:Actually, I wouldn't use a Queue to model a real Queue to begin with. I know it's shocking, but occasionally, people have been known to leave a queue from between the queue.

Again, you could be falling into the trap of trying to model the real world to a T. If there were such a requirement, then maybe you'd need an enhanced queue that allows you to remove elements other than the head to simulate someone leaving the queue before they get to the front. I wouldn't abandon the use of a queue just for that reason though.

Another approach might be to have a method in the object such as 'stillThere()' which calculates whether or not that "Person" would have waited in line long enough to get serviced. The person could be passed the current iteration number when it gets added to the queue and when it gets to the head of the queue, stillThere() could be passed the current iteration number. If the difference between the two iteration numbers is below a (random?) threshold, then stillThere() returns true, otherwise the "Person" can be deemed to have left the queue and you just remove it and get the next Person. This doesn't model the real world exactly but I think it's a perfectly fine design for a program.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

To be most helpful to the OP, I think we need to stick with the core requirements as he gave them and not introduce scope creep by speculating on other scenarios or extensions to the requirements.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Junilu Lacar wrote:So what if you leave the element that you are processing on the queue while you are working with it? What's wrong with waiting until you're done with processing the head element before removing it from the queue?

Because:
1. The "busy" time has absolutely nothing to do with the operation of the Queue (or queue). It simply signals whether a Customer entering the store needs to join the queue, or the person at the front can leave it.
2. When coming up with a rationalization of a problem, it makes sense to use real life examples.
3. Having a Cashier (or server or till; or whatever you want to call it) provides a natural point at which to apply the "busy" counter; it also allows for the case that I outlined in my previous post.
4. The model allows for more than one point of service independent of the number of queues.

I think the way the problem is stated is good enough to achieve a reasonable approximation.

5. I don't see anything in the instructions that precludes the option of a Cashier.

I totally agree with you about queues and Queues though.

Winston
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

Junilu Lacar wrote:Paul and Winston: I won't argue that the approaches you would take are valid in the real world but I don't see how it makes much of a difference in a program. As I pointed out before (and they are not my own words), an OO design does not necessarily have to mimic the real world to a T.

So what if you leave the element that you are processing on the queue while you are working with it? What's wrong with waiting until you're done with processing the head element before removing it from the queue? You're only dealing with object references after all.


Well, for one thing I find it easier to think of it that way. And in this particular case Raymond chose to use an immutable object representing an integer, when the rules said you had to change the object. So dealing with an object reference is part of the problem in this case. It would be much easier to extract the Integer from the queue and make it an int, which is nicely mutable. In this particular case, that is.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Junilu,

I've split our discussion off into a new topic, as we seem to have wandered off-brief.

Hope you don't mind.

Winston
 
Consider Paul's rocket mass heater.
 
subject: Modify the first element in a queue?
 
Similar Threads
need help making an applet work to display info..
need help in understanding Comparator ?
Blocking while waiting for record lock to clear
Nested loop
ConcurrentLinkedQueue ??