Is there a collection data structure in Java (and C# if you know) that can be iterated over, with the following properties:
- The current element can be removed without affecting the current iterator (the rest of the already-started iterator's iterations).
- New elements can be added, but will also not affect the current iterator—not be included as an iterated value while the current iterator's iteration is still going. In my case only one new element would be added per iteration, but none should be seen until a new iterator is fetched from the iterable.
- The order of elements doesn’t matter.
In effect, there is an incoming list and an outgoing list of items. The incoming list is iterated and some are copied to a new list. Some new elements can be added to the new list during iteration. After the iteration is over, the old incoming list is replaced with the new outgoing list. This whole process is itself within a loop.
So, copying the elements to a newly constructed collection object each time seems inefficient compared to one that had these add/remove properties.
I was kind of thinking of some kind of queue that lets me preview the current item and then either dequeue it or not, and move onto the next item. And I can add more items to the head of the queue but won’t see them because I’m moving toward the end. A doubly linked list could have these properties, right?
If you really want to know what it’s for, it’s to soup up the second large code block in an answer of mine.