Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36275

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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Knute Johnson <nospam@rabbitbrush.frazmtn.com>
Newsgroups comp.lang.java.programmer
Subject Removing an element while iterating over a List with enhanced for?
Date Wed, 15 Feb 2017 16:56:23 -0600
Organization A noiseless patient Spider
Lines 36
Message-ID <o82m7a$tls$1@dont-email.me> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Wed, 15 Feb 2017 22:54:35 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="b0b2173ee00447110bf0309bc7ae0bf9"; logging-data="30396"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1962RFYwYTImnhK6b5aW7mC"
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1
X-Mozilla-News-Host news://news.eternal-september.org:119
Cancel-Lock sha1:iRi+rk1Hf1qWFXnG5sU/vK3Z84Y=
Xref csiph.com comp.lang.java.programmer:36275

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-15 16:56 -0600
  Re: Removing an element while iterating over a List with enhanced for? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-02-15 18:24 -0500
    Re: Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-15 17:51 -0600
  Re: Removing an element while iterating over a List with enhanced for? Arne Vajhøj <arne@vajhoej.dk> - 2017-02-15 22:43 -0500
    Re: Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-16 11:17 -0600
      Re: Removing an element while iterating over a List with enhanced for? Robert Klemme <shortcutter@googlemail.com> - 2017-02-16 18:30 +0100
        Re: Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-16 11:35 -0600
        Re: Removing an element while iterating over a List with enhanced for? Arne Vajhøj <arne@vajhoej.dk> - 2017-02-16 14:02 -0500
        Re: Removing an element while iterating over a List with enhanced for? v_borchert@despammed.com (Volker Borchert) - 2017-02-16 20:09 +0000
          Re: Removing an element while iterating over a List with enhanced for? Robert Klemme <shortcutter@googlemail.com> - 2017-02-16 23:21 +0100

csiph-web