Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing an element while iterating over a List with enhanced for?

22 views
Skip to first unread message

Knute Johnson

unread,
Feb 15, 2017, 5:56:42 PM2/15/17
to
An enhanced for loop on a List is no different than using an Iterator
right? So is there any problem removing the current element from the
list while using the enhanced for loop? I couldn't find anything that
said it was OK or not OK in the JLS.

import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.*;

public class test9 {
public static void main(String... args) {
List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());

for (Integer i : list)
if (i == 9)
list.remove(i);

list.forEach(System.out::println);

list = Stream.of(1,5,7,9,13).collect(toList());

Iterator<Integer> iter = list.iterator();

while (iter.hasNext())
if (iter.next() == 9)
iter.remove();

list.forEach(System.out::println);
}
}

Thanks,

--

Knute Johnson

Eric Sosman

unread,
Feb 15, 2017, 6:24:13 PM2/15/17
to
On 2/15/2017 5:56 PM, Knute Johnson wrote:
> An enhanced for loop on a List is no different than using an Iterator
> right? So is there any problem removing the current element from the
> list while using the enhanced for loop? I couldn't find anything that
> said it was OK or not OK in the JLS.

1) During an iteration, you can only remove elements through
the Iterator's own remove() method.

2) The enhanced `for' does indeed use an Iterator, but it
doesn't expose the Iterator instance: You have no way to refer
to it.

3) Since you can't get a reference to the Iterator, you can't
call the Iterator's remove().

Therefore,

4) You can't remove anything while executing an enhanced `for'.

> List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());
>
> for (Integer i : list)
> if (i == 9)
> list.remove(i);

Not permitted. From the Javadoc for Iterator#remove: "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."

Just for fun, I tried your example -- and it worked!!! But that's
one of the possible outcomes of "unspecified" behavior, and I rather
suspected that it "worked" because the removed element just happened
to be the penultimate and the Iterator's hasNext() might have returned
false. So I changed `9' to `7' to attack an earlier element, and got

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at esosman.misc.Knute.main(Knute.java:15)

> while (iter.hasNext())
> if (iter.next() == 9)
> iter.remove();

This is the correct approach.

--
eso...@comcast-dot-net.invalid
One thousand four hundred thirty-five days to go.

Knute Johnson

unread,
Feb 15, 2017, 6:51:34 PM2/15/17
to
Thanks!
--

Knute Johnson

Arne Vajhøj

unread,
Feb 15, 2017, 10:43:48 PM2/15/17
to
On 2/15/2017 5:56 PM, Knute Johnson wrote:
> An enhanced for loop on a List is no different than using an Iterator
> right? So is there any problem removing the current element from the
> list while using the enhanced for loop? I couldn't find anything that
> said it was OK or not OK in the JLS.

> List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());
>
> for (Integer i : list)
> if (i == 9)
> list.remove(i);
>
> list.forEach(System.out::println);
>
> list = Stream.of(1,5,7,9,13).collect(toList());
>
> Iterator<Integer> iter = list.iterator();
>
> while (iter.hasNext())
> if (iter.next() == 9)
> iter.remove();
>
> list.forEach(System.out::println);

Since you are already using streams and lambdas then why not:

list = list.stream().filter(v -> v != 9).collect(toList());

?

Arne


Knute Johnson

unread,
Feb 16, 2017, 12:17:46 PM2/16/17
to
That's where I'm going eventually. The real code is a lot more complex
and I got distracted by something that shouldn't have worked :-).

--

Knute Johnson

Robert Klemme

unread,
Feb 16, 2017, 12:30:12 PM2/16/17
to
On 16.02.2017 18:17, Knute Johnson wrote:
> On 2/15/2017 21:43, Arne Vajhøj wrote:
>> On 2/15/2017 5:56 PM, Knute Johnson wrote:
>>> An enhanced for loop on a List is no different than using an Iterator
>>> right? So is there any problem removing the current element from the
>>> list while using the enhanced for loop? I couldn't find anything that
>>> said it was OK or not OK in the JLS.
>>
>>> List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());

Btw. I know it is just the code to get a list filled but still you can
have that much simpler:

list = Arrays.asList(1, 5, 7, 9, 13);

We tend to forget the "good ole code" over all the fancy new things. :-)

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Knute Johnson

unread,
Feb 16, 2017, 12:35:43 PM2/16/17
to
On 2/16/2017 11:30, Robert Klemme wrote:
> On 16.02.2017 18:17, Knute Johnson wrote:
>> On 2/15/2017 21:43, Arne Vajhøj wrote:
>>> On 2/15/2017 5:56 PM, Knute Johnson wrote:
>>>> An enhanced for loop on a List is no different than using an Iterator
>>>> right? So is there any problem removing the current element from the
>>>> list while using the enhanced for loop? I couldn't find anything that
>>>> said it was OK or not OK in the JLS.
>>>
>>>> List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());
>
> Btw. I know it is just the code to get a list filled but still you can
> have that much simpler:
>
> list = Arrays.asList(1, 5, 7, 9, 13);
>
> We tend to forget the "good ole code" over all the fancy new things. :-)
>
> Cheers
>
> robert
>
>

It was a bad day yesterday, that's all I can say.

--

Knute Johnson

Arne Vajhøj

unread,
Feb 16, 2017, 2:02:33 PM2/16/17
to
On 2/16/2017 12:30 PM, Robert Klemme wrote:
> On 16.02.2017 18:17, Knute Johnson wrote:
>> On 2/15/2017 21:43, Arne Vajhøj wrote:
>>> On 2/15/2017 5:56 PM, Knute Johnson wrote:
>>>> An enhanced for loop on a List is no different than using an Iterator
>>>> right? So is there any problem removing the current element from the
>>>> list while using the enhanced for loop? I couldn't find anything that
>>>> said it was OK or not OK in the JLS.
>>>
>>>> List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());
>
> Btw. I know it is just the code to get a list filled but still you can
> have that much simpler:
>
> list = Arrays.asList(1, 5, 7, 9, 13);
>
> We tend to forget the "good ole code" over all the fancy new things. :-)

Note though that Java 9:

list = List.of(1, 5, 7, 9, 13);

will not work!

Arne


Volker Borchert

unread,
Feb 16, 2017, 4:02:56 PM2/16/17
to
Robert Klemme wrote:
> On 16.02.2017 18:17, Knute Johnson wrote:
> > On 2/15/2017 21:43, Arne Vajh??j wrote:
> >> On 2/15/2017 5:56 PM, Knute Johnson wrote:
> >>> An enhanced for loop on a List is no different than using an Iterator
> >>> right? So is there any problem removing the current element from the
> >>> list while using the enhanced for loop? I couldn't find anything that
> >>> said it was OK or not OK in the JLS.
> >>
> >>> List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());
>
> Btw. I know it is just the code to get a list filled but still you can
> have that much simpler:
>
> list = Arrays.asList(1, 5, 7, 9, 13);
>
> We tend to forget the "good ole code" over all the fancy new things. :-)

The list you get from this does not support removal.

--

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mc...@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert <v_bor...@despammed.com>

Robert Klemme

unread,
Feb 16, 2017, 5:21:41 PM2/16/17
to
On 16.02.2017 21:09, Volker Borchert wrote:
> Robert Klemme wrote:
>> On 16.02.2017 18:17, Knute Johnson wrote:
>>> On 2/15/2017 21:43, Arne Vajh??j wrote:
>>>> On 2/15/2017 5:56 PM, Knute Johnson wrote:
>>>>> An enhanced for loop on a List is no different than using an Iterator
>>>>> right? So is there any problem removing the current element from the
>>>>> list while using the enhanced for loop? I couldn't find anything that
>>>>> said it was OK or not OK in the JLS.
>>>>
>>>>> List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());
>>
>> Btw. I know it is just the code to get a list filled but still you can
>> have that much simpler:
>>
>> list = Arrays.asList(1, 5, 7, 9, 13);
>>
>> We tend to forget the "good ole code" over all the fancy new things. :-)
>
> The list you get from this does not support removal.

Correct. Slightly less simple

list = new ArrayList<>(Arrays.asList(1, 5, 7, 9, 13));

I concede it's not so nice any more. :-)
0 new messages