4 Replies - 975 Views - Last Post: 01 April 2011 - 12:25 AM Rate Topic: -----

#1 Yogesh_P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-March 11

Updating an custom Object stored in blocking priority queue

Posted 31 March 2011 - 08:48 PM

Hi

I have a blocking priority queue which stores Objects of type Message, message has String[] data = new String[10]. Now I have to iterate over whole blocking Queue, check if its Object message's 2nd element is equal to 6th element of an incoming message.

The comparator of Messages is not based on 6th element which needs to be updated. Problem is that if I take out an object then how to put it at same position and if I use the code below to update it then anytime iter.next() is run it may start pointing to next Object.

Here is what I am trying.

public synchronized void updateAck(Message ackMessage)
	{
		Iterator iter  = localQ.iterator(); // localQ is the blocking priority queue here
		while(iter.hasNext())
		{
			if(((Message)iter.next()).data[2].equalsIgnoreCase(ackMessage.data[6]))
			{
				(Integer.parseInt((Message)iter.next()).data[6])+1);

			}
		}
	}



Another implementation that comes to my point is to remove the head element, update it and then put it back. But I am afraid that in that case iter.hasNext() will go into an infinite loop as element removed from head will be added to tail of the queue. Also should I keep it synchronized ?

Any pointers will be very helpful.

Thanks and Regards

Is This A Good Question/Topic? 0
  • +

Replies To: Updating an custom Object stored in blocking priority queue

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,608
  • Joined: 27-December 08

Re: Updating an custom Object stored in blocking priority queue

Posted 31 March 2011 - 08:58 PM

A couple things. First, Java Collections and their Iterators are generic. So you should be storing a PriorityBlockingQueue<Message> and using an Iterator<Message>. That elminiates the need for you to cast.

Second, you are dealing with two separate Message objects at these lines, since the Iterator next() method returns an Object, then moves the buffer to the next one. Better to assign iter.next() to a Message variable first thing in your loop, then deal with that variable.
			if(((Message)iter.next()).data[2].equalsIgnoreCase(ackMessage.data[6]))
			{
				(Integer.parseInt((Message)iter.next()).data[6])+1);




Quote

Problem is that if I take out an object then how to put it at same position and if I use the code below to update it then anytime iter.next() is run it may start pointing to next Object

An Iterator doesn't remove elements from the PriorityBlockingQueue. So ordering won't change.
Was This Post Helpful? 1
  • +
  • -

#3 Yogesh_P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-March 11

Re: Updating an custom Object stored in blocking priority queue

Posted 31 March 2011 - 09:05 PM

I have made Blocking queue which can store Objects of type Message only, I didn't knew about Iterators, Thanks. If I store an Message from iter.next() into an temp Message then how can I replace it with the newer updated copy.

Quote

An Iterator doesn't remove elements from the PriorityBlockingQueue. So ordering won't change.


But when I am putting new element back, wont my queue put it at the end of queue, after successive comparisons will it be put back into its original position.

Regards
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,608
  • Joined: 27-December 08

Re: Updating an custom Object stored in blocking priority queue

Posted 31 March 2011 - 09:07 PM

If you modify the Message from the Iterator, you aren't removing it then adding it back. When you assign the iter.next() call to a Message object, you aren't creating a new Object. You are having a variable reference an already existing object in memory and modify that.
Was This Post Helpful? 1
  • +
  • -

#5 Yogesh_P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-March 11

Re: Updating an custom Object stored in blocking priority queue

Posted 01 April 2011 - 12:25 AM

Thanks again, I see your point. Here is the modified version that I guess should work.......
(I need to compare 7th element of incoming message with 3rd element of existing message hence 2,5 as array index)

Should I synchronize it or it would be fine without it as it is blocking priority Queue ?

public synchronized void updateAck(Message ackMessage)
	{
		Iterator<Message> iter  = localQ.iterator();
		while(iter.hasNext())
		{
			Message temp = iter.next();
			String prevTimeStamp = temp.data[2];
			if(temp.data[2].equalsIgnoreCase(ackMessage.data[6]))
			{
				temp.data[5] = Integer.toString(Integer.parseInt(temp.data[5]) + 1);

			}
		}
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1