Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two issues here:</p> <p>The first issue is, adding to an <code>Collection</code> after an <code>Iterator</code> is returned. As mentioned, there is no defined behavior when the underlying <code>Collection</code> is modified, as noted in the documentation for <code>Iterator.remove</code>:</p> <blockquote> <p>... The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.</p> </blockquote> <p>The second issue is, even if an <code>Iterator</code> could be obtained, and then return to the same element the <code>Iterator</code> was at, there is no guarantee about the order of the iteratation, as noted in the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html#iterator()" rel="noreferrer"><code>Collection.iterator</code></a> method documentation:</p> <blockquote> <p>... There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).</p> </blockquote> <p>For example, let's say we have the list <code>[1, 2, 3, 4]</code>.</p> <p>Let's say <code>5</code> was added when the <code>Iterator</code> was at <code>3</code>, and somehow, we get an <code>Iterator</code> that can resume the iteration from <code>4</code>. However, there is no guarentee that <code>5</code> will come after <code>4</code>. The iteration order may be <code>[5, 1, 2, 3, 4]</code> -- then the iterator will still miss the element <code>5</code>.</p> <p>As there is no guarantee to the behavior, one cannot assume that things will happen in a certain way.</p> <p>One alternative could be to have a separate <code>Collection</code> to which the newly created elements can be added to, and then iterating over those elements:</p> <pre><code>Collection&lt;String&gt; list = Arrays.asList(new String[]{"Hello", "World!"}); Collection&lt;String&gt; additionalList = new ArrayList&lt;String&gt;(); for (String s : list) { // Found a need to add a new element to iterate over, // so add it to another list that will be iterated later: additionalList.add(s); } for (String s : additionalList) { // Iterate over the elements that needs to be iterated over: System.out.println(s); } </code></pre> <p><strong>Edit</strong></p> <p>Elaborating on <a href="https://stackoverflow.com/questions/993025/java-adding-elements-to-a-collection-during-iteration/993036#993036">Avi's answer</a>, it is possible to queue up the elements that we want to iterate over into a queue, and remove the elements while the queue has elements. This will allow the "iteration" over the new elements in addition to the original elements.</p> <p>Let's look at how it would work.</p> <p>Conceptually, if we have the following elements in the queue:</p> <p><code>[1, 2, 3, 4]</code></p> <p>And, when we remove <code>1</code>, we decide to add <code>42</code>, the queue will be as the following:</p> <p><code>[2, 3, 4, 42]</code></p> <p>As the queue is a <a href="http://en.wikipedia.org/wiki/FIFO" rel="noreferrer">FIFO</a> (first-in, first-out) data structure, this ordering is typical. (As noted in the documentation for the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html" rel="noreferrer"><code>Queue</code></a> interface, this is not a necessity of a <code>Queue</code>. Take the case of <a href="http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html" rel="noreferrer"><code>PriorityQueue</code></a> which orders the elements by their natural ordering, so that's not FIFO.)</p> <p>The following is an example using a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html" rel="noreferrer"><code>LinkedList</code></a> (which is a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html" rel="noreferrer"><code>Queue</code></a>) in order to go through all the elements along with additional elements added during the dequeing. Similar to the example above, the element <code>42</code> is added when the element <code>2</code> is removed:</p> <pre><code>Queue&lt;Integer&gt; queue = new LinkedList&lt;Integer&gt;(); queue.add(1); queue.add(2); queue.add(3); queue.add(4); while (!queue.isEmpty()) { Integer i = queue.remove(); if (i == 2) queue.add(42); System.out.println(i); } </code></pre> <p>The result is the following:</p> <pre><code>1 2 3 4 42 </code></pre> <p>As hoped, the element <code>42</code> which was added when we hit <code>2</code> appeared.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload