Best collection for sorted, changing list?

I have a list of objects that are sorted by specific criteria. The main processing loop iterates over this list. However, during processing, there can be new objects created that need to be sorted into this list.

First I used an ArrayList, but got a ConcurrentModificationException on the next iteration after I inserted a new object. Then I thought about Queues but can't figure out what is the best Queue for my requirement.

Or is it best to not have a changing list but always a current list that can change and process objects are removed so that I always can get the first element for the next main iteration?

[634 byte] By [MartinHilperta] at [2007-10-3 4:21:55]
# 1
Use Vector if you want to add stuff while working with the list.
SoulTech2012a at 2007-7-14 22:24:10 > top of Java-index,Core,Core APIs...
# 2
That won't help.If you want to modify the collection while you're iterating you must use the methods of the Iterator to do so. In your case ListIterator.add() is what you want.
ejpa at 2007-7-14 22:24:10 > top of Java-index,Core,Core APIs...
# 3

> That won't help.

>

> If you want to modify the collection while you're

> iterating you must use the methods of the Iterator to

> do so. In your case ListIterator.add() is what you

> want.

That's not enough as the new items need to be sorted into the list - not just added at the end ...

MartinHilperta at 2007-7-14 22:24:10 > top of Java-index,Core,Core APIs...
# 4
java.util.TreeSet. That's your ticket.
Updownquarka at 2007-7-14 22:24:10 > top of Java-index,Core,Core APIs...
# 5
But how to iterate over the TreeSet items and constantly adding items? The TreeSet's Iterator can remove but not add items.
MartinHilperta at 2007-7-14 22:24:10 > top of Java-index,Core,Core APIs...
# 6

> But how to iterate over the TreeSet items and

> constantly adding items? The TreeSet's Iterator can

> remove but not add items.

If you need to add items while iterating as well, then your problem is not a simple one. When you add items into a sorted set, it's not immediately clear where the item will go. If you are iterating along and all of a sudden add an item to the set through the iterator, the new item may go behind the iterator's position in the set, invalidating the iterator (an iterator is considered invalid if it misses an element in the collection). It appears the JDK did not provide a SortedSet implementation that is up to your challenge and I can't think myself how to solve this problem while keeping the contracts of java.util.Collection and java.util.Iterator.

What you may need to do is find a way to finish modifying your collection before you iterate over it, or possibly abandon the use of iterators and just use get operations. I don't know what your system requires, but it sounds like it will need to be rethought.

Updownquarka at 2007-7-14 22:24:10 > top of Java-index,Core,Core APIs...
# 7

Probably you can use CopyOnWriteArrayList class for having concurrent updates during tranversal.

Iterator obtained for this class will just return a copy of array of the collection when iterator is initialized, additions and deletions of elements is not reflected during the iterator traversal.

I have put a workaround by refreshing the iterator after adding by initializing it again.

CopyOnWriteArrayList<Integer> ts = new CopyOnWriteArrayList<Integer>();

ts.add(new Integer(1));

ts.add(new Integer(6));

ts.add(new Integer(4));

ts.add(new Integer(2));

int index = 0;

for(Iterator<Integer> it=ts.iterator();it.hasNext();){

System.out.println(it.next());

if(index ==0){

ts.add(new Integer(5));

it=ts.subList(++index,ts.size()).iterator();

}

}

Vchutkia at 2007-7-14 22:24:10 > top of Java-index,Core,Core APIs...
# 8

Well, I tried TreeSet (as I just need a set and not a map) and I get a ConcurrentModificationException the next time I call iterator.next() after I added a new item. :-(

(BTW: I have the "lucky" task to migrate VB code to Java and in the VB project they iterate over a list and change this list during iteration - so it works in VB. The logic problem is that the list items are transactions (sorted by date) and during the processing of a transaction, new transactions can get generated and sorted into the global list. It seems I need to create a separate list of new transactions and check in my global (unchangeable) iteration if there's a new transaction in my separate list to process first ...)

MartinHilperta at 2007-7-14 22:24:11 > top of Java-index,Core,Core APIs...
# 9
Only thing I can think of is when something needs to added, place it in a temp storage. Once the iterating over the collection has completed, call another method that checks the temp storage and inserts any data there. Then you can return to iterating over the list again.
floundera at 2007-7-14 22:24:11 > top of Java-index,Core,Core APIs...