1 2 3 4 Previous Next 50 Replies Latest reply: Apr 25, 2012 9:44 AM by Owen Thomas Go to original post RSS
      • 15. Re: Why the ConcurrentModificationException?
        796440
        Owen Thomas wrote:
        EJP wrote:
        How is foreach any less necessary than a while loop?
        It is 'less necessary' when it causes you to write code that throws ConcurrentModificationExceptions, and spend time on a forum because you couldn't figure it out, and use extra collections: none of which would have happened if you had used the while loop.
        What I feel is necessary is for me to understand when and why an iterator would be good choice.
        Using an explicit Iterator is a good choice when you want to modify the Collection during iteration.

        Using an implicit Iterator (via the foreach loop) is a good choice any other time you want to iterate over the Collection.
        • 16. Re: Why the ConcurrentModificationException?
          EJP
          It is 'less necessary' when it causes you to write code that throws ConcurrentModificationExceptions, and spend time on a forum because you couldn't figure it out, and use extra collections: none of which would have happened if you had used the while loop.
          What I feel is necessary is for me to understand when and why an iterator would be good choice.
          It would be a good choice when you don't want to get a ConcurrentModificationException, don't want to add O(N) to your execution time by using an extra collection, and don't want to waste everybody's time on a forum.
          • 17. Re: Why the ConcurrentModificationException?
            Owen Thomas
            jverd wrote:
            Using an explicit Iterator is a good choice when you want to modify the Collection during iteration.

            Using an implicit Iterator (via the foreach loop) is a good choice any other time you want to iterate over the Collection.
            Yea, I understand that you want to impress upon me the importance of using an iterator, but I don't see the natural advantage... surely, as EJP says, there is no additional linear execution time to my algorithm.

            Why is this so? I need you to go an extra step to justify these assertions you are making.

            Now, an article explaining this point would be excellent. I think it would make EJP happy too.
            • 18. Re: Why the ConcurrentModificationException?
              796440
              Owen Thomas wrote:
              jverd wrote:
              Using an explicit Iterator is a good choice when you want to modify the Collection during iteration.

              Using an implicit Iterator (via the foreach loop) is a good choice any other time you want to iterate over the Collection.
              Yea, I understand that you want to impress upon me the importance of using an iterator, but I don't see the natural advantage... surely, as EJP says, there is no additional linear execution time to my algorithm.
              Eh? He's saying there is additional execution time, as I am saying. How is it that you think that creating a copy of a collection takes zero time.
              Why is this so? I need you to go an extra step to justify these assertions you are making.
              Ignoring the arrogance of that comment for a moment, why would I need to justify to you that duplicating a collection takes more time than not duplicating a collection?
              Now, an article explaining this point would be excellent.
              I've already pointed you to the javadocs. You also have access to google. I don't even know what point it is on which you need more information.
              I think it would make EJP happy too.
              EJP already understands how collections work, and despite what you seem to think, he's agreeing with me.
              • 19. Re: Why the ConcurrentModificationException?
                EJP
                surely, as EJP says, there is no additional linear execution time to my algorithm.
                I most certainly said no such thing. How can copying a collection possibly take zero time? Please try to make some sense.
                Now, an article explaining this point would be excellent. I think it would make EJP happy too.
                And please stop putting words into other people's mouths. I'm perfectly 'happy' already. I have myself twice stated some of the reasons to use an iterator over a foreach loop. When they don't apply, use the for-each of course. Why you are still asking for the same information is beyond me.

                You seem to be just ignoring what you're being told here, when you're not wilfully misreading it.
                • 20. Re: Why the ConcurrentModificationException?
                  Owen Thomas
                  jverd wrote:
                  Yea, I understand that you want to impress upon me the importance of using an iterator, but I don't see the natural advantage... surely, as EJP says, there is no additional linear execution time to my algorithm.
                  Eh? He's saying there is additional execution time, as I am saying. How is it that you think that creating a copy of a collection takes zero time.
                  You're confused (probably by my carelessness, I should have completed my sentence): ... there is no additional linear execution time to my algorithm [if I used an Iterator as you and EJP suggest].

                  However, I don't want to just take your word for it. I want to know why, and I have the luxury of asking this question.

                  >
                  Why is this so? I need you to go an extra step to justify these assertions you are making.
                  Why would I need to justify to you that duplicating a collection takes more time than not duplicating a collection?
                  Because you're just telling me that if I used an Iterator, I wouldn't be duplicating a collection. I can see that you are saying that. But why should I just take your word for it.

                  More to the point, what connection (if any needs to exist) between [edit: an Iterator] not duplicating a collection and not throwing a ConcurrentModificationException?

                  >
                  Now, an article explaining this point would be excellent. I think it would make EJP happy too.
                  I've already pointed you to the javadocs. You also have access to google. And EJP already understand how collections work, and despite what you seem to think, he's agreeing with me.
                  The javadocs just tell me that it is so. I want to know why it is so in terms of other things I already accept. The O(N) explanation is almost there, but I want to know three more things: 1. why doesn't an Iterator need to create a duplicate copy of the collection, 2. why is it that creating an Iterator allows iteration over a collection even while it is being modified and a foreach loop doesn't permit iteration while the collection is being modified, and 3. are these two qualities naturally linked in some way?

                  Edited by: Owen Thomas on Apr 24, 2012 10:56 PM
                  • 21. Re: Why the ConcurrentModificationException?
                    796440
                    Owen Thomas wrote:
                    jverd wrote:
                    Yea, I understand that you want to impress upon me the importance of using an iterator, but I don't see the natural advantage... surely, as EJP says, there is no additional linear execution time to my algorithm.
                    Eh? He's saying there is additional execution time, as I am saying. How is it that you think that creating a copy of a collection takes zero time.
                    You're confused (probably by my carelessness, I should have completed my sentence): ... there is no additional linear execution time to my algorithm [if I used an Iterator as you and EJP suggest].
                    By "my algorithm", you mean creating a copy of the collection, right?

                    If so, then, again: How do you not see that creating a copy takes more time than not creating a copy?
                    However, I don't want to just take your word for it. I want to know why, and I have the luxury of asking this question.
                    Why what? What exactly do you not undertsand?

                    >
                    >>
                    Why is this so? I need you to go an extra step to justify these assertions you are making.
                    Why would I need to justify to you that duplicating a collection takes more time than not duplicating a collection?
                    Because you're just telling me that if I used an Iterator, I wouldn't be duplicating a collection. I can see that you are saying that. But why should I just take your word for it.
                    You don't have to take my word for it. I already pointed you to the javadocs. And you can easily prove it to yourself.
                    List list = new ArrayList();
                    list.add("a");
                    System.out.println(list);
                    Iterator it = list.iterator();
                    it.next();
                    it.remove();
                    System.out.println(list);
                    If you run that, you'll see that the second print out of list is empty. If calling iterator() created a second list, then the original one would not have an element removed from it.

                    More to the point, what connection (if any needs to exist) between not duplicating a collection and not throwing a ConcurrentModificationException?
                    * sigh *

                    What???
                    The javadocs just tell me that it is so. I want to know why it is so in terms of other things I already accept.
                    Then you're on your own. The javadocs are the final word on the API's behavior.
                    1. why doesn't an Iterator need to create a duplicate copy of the collection,
                    Why would it? As I told you before, the only point you mentioned--modifying during iteration--is why CME is thrown.
                    2. why is it that creating an Iterator allows iteration over a collection even while it is being modified
                    It only allows modification through the Iterator itself. Since the Iterator is doing the modification, it knows exactly what state changes the collection has undergone, so it won't get "out of sync", so to speak. I already said this earlier.
                    and a foreach loop doesn't permit iteration while the collection is being modified
                    Any modification to a collection during iteration, other than through the Iterator itself gives CME. (I already said this too.) In the case of the foreach loop, it's using an Iterator, but it's behind the scenes, and we don't have access to that Iterator, so, obviously, any attempt to modify that collection will not be through use of that Iterator.
                    • 22. Re: Why the ConcurrentModificationException?
                      EJP
                      You're confused (probably by my carelessness, I should have completed my sentence): ... there is no additional linear execution time to my algorithm [if I used an Iterator as you and EJP suggest].
                      Exactly what we've been telling you, and exactly why you should use an Iterator in this situation rather than copying the collection.

                      But you stated above that your algorithm consisted of making a copy and using for-each over that, so how that magically now involves using an Iterator as everybody has been telling you, and which you have been resisting learning, is a mystery.
                      However, I don't want to just take your word for it. I want to know why, and I have the luxury of asking this question.
                      What question?
                      Because you're just telling me that if I used an Iterator, I wouldn't be duplicating a collection. I can see that you are saying that. But why should I just take your word for it.
                      I do not understand. You said you had found a solution involving copying the collection. Using the iterator you don't have to copy the collection. What on earth are you talking about now? Not copying a collection is faster than copying a collection. Can we agree on that?
                      More to the point, what connection (if any needs to exist) between [edit: an Iterator] not duplicating a collection and not throwing a ConcurrentModificationException?
                      I've just said all that. If you use an Iterator, you can (a) use Iterator.remove() which doesn't throw ConcurrentModificationException, and (b) you don't have to copy the collection just so you can keep using the for-each loop. Is this getting clear?
                      I want to know three more things: 1. why doesn't an Iterator need to create a duplicate copy of the collection
                      Because (a) nowhere in the Javadoc does it say that it does, (b) it is so (c) why should it?
                      2. why is it that creating an Iterator allows iteration over a collection even while it is being modified and a foreach loop doesn't permit iteration while the collection is being modified
                      Because that's the way it's implemented. It knows where it is in the collection, it knows when you call Iterator.remove(), and it can adjust its internal state accordingly. If you use the foreach loop you are still using an implicit iterator, but you aren't calling Iterator.remove(), because you can't, so the iterator doesn't know that you've called it, so it can't adjust its internal state, so it detects the inconsistency and throws ConcurrentModificationException, which is what it says it does in the Javadoc.
                      3. are these two qualities naturally linked in some way?
                      They are linked by being part of the contract of Iterator, as described in the Javadoc.
                      • 23. Re: Why the ConcurrentModificationException?
                        Owen Thomas
                        jverd wrote:
                        You don't have to take my word for it. I already pointed you to the javadocs. And you can easily prove it to yourself.
                        List list = new ArrayList();
                        list.add("a");
                        System.out.println(list);
                        Iterator it = list.iterator();
                        it.next();
                        it.remove();
                        System.out.println(list);
                        If you run that, you'll see that the second print out of list is empty. If calling iterator() created a second list, then the original one would not have an element removed from it.
                        That would explain things a bit more to me. I thought I was removing from the original collection.
                        2. why is it that creating an Iterator allows iteration over a collection even while it is being modified
                        It only allows modification through the Iterator itself. Since the Iterator is doing the modification, it knows exactly what state changes the collection has undergone, so it won't get "out of sync", so to speak. I already said this earlier.
                        The explicit mention of this detail is informative. Maybe, assuming you did indeed say it earlier, I wasn't ready to accept it.

                        >
                        and a foreach loop doesn't permit iteration while the collection is being modified
                        Any modification to a collection during iteration, other than through the Iterator itself gives CME. (I already said this too.) In the case of the foreach loop, it's using an Iterator, but it's behind the scenes, and we don't have access to that Iterator, so, obviously, any attempt to modify that collection will not be through use of that Iterator.
                        Again... I imagined that would have created the Iterator and then removed the element in the collection by calling the remove method on the collection itself. I see now that the element must be removed through the Iterator's interface.
                        • 25. Re: Why the ConcurrentModificationException?
                          796440
                          Owen Thomas wrote:
                          jverd wrote:
                          You don't have to take my word for it. I already pointed you to the javadocs. And you can easily prove it to yourself.
                          List list = new ArrayList();
                          list.add("a");
                          System.out.println(list);
                          Iterator it = list.iterator();
                          it.next();
                          it.remove();
                          System.out.println(list);
                          If you run that, you'll see that the second print out of list is empty. If calling iterator() created a second list, then the original one would not have an element removed from it.
                          That would explain things a bit more to me. I thought I was removing from the original collection.
                          :headdesk:

                          You ARE removing from the original collection. That's the whole point. Since the original collection is modified by calling Iterator.remove(), that shows that the Iterator is operating on the original collection, as EJP and I have been telling you and NOT on a copy, as you have been incorrectly assuming.

                          Are you even trying to understand what's being said to you?
                          2. why is it that creating an Iterator allows iteration over a collection even while it is being modified
                          It only allows modification through the Iterator itself. Since the Iterator is doing the modification, it knows exactly what state changes the collection has undergone, so it won't get "out of sync", so to speak. I already said this earlier.
                          The explicit mention of this detail is informative. Maybe, assuming you did indeed say it earlier, I wasn't ready to accept it.
                          If you're not ready to accept the answer, don't ask the question. Why should we have to repeat ourselves just because you're too stubborn to let go of your unfounded assumptions?
                          and a foreach loop doesn't permit iteration while the collection is being modified
                          Any modification to a collection during iteration, other than through the Iterator itself gives CME. (I already said this too.) In the case of the foreach loop, it's using an Iterator, but it's behind the scenes, and we don't have access to that Iterator, so, obviously, any attempt to modify that collection will not be through use of that Iterator.
                          Again... I imagined that would have created the Iterator and then removed the element in the collection by calling the remove method on the collection itself.
                          So did you not read the relevant docs? Did it not occur to you that that might be a good idea? Or were you just "not ready to accept" when they said that removing via the Iterator is okay, and modifications not through the Iterator are not ok?
                          I see now that the element must be removed through the Iterator's interface.
                          Which I had previously told you, as did at least one other person, and the docs would have if you'd bothered to spend a fraction as much time reading them as you did arguing without basis here.
                          • 26. Re: Why the ConcurrentModificationException?
                            Owen Thomas
                            jverd wrote:
                            :headdesk:
                            xx

                            It probably will happen again. :)
                            • 27. Re: Why the ConcurrentModificationException?
                              EJP
                              The explicit mention of this detail is informative.
                              The explicit mention of this detail is entirely obvious from the Javadoc. "Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics."
                              • 28. Re: Why the ConcurrentModificationException?
                                gimbal2
                                EJP wrote:
                                The explicit mention of this detail is informative.
                                The explicit mention of this detail is entirely obvious from the Javadoc. "Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics."
                                Yes but its written on the internet, that is not to be trusted. Don't you understand that !?!?!?

                                Why does there have to be a thread like this at least once a week? If you don't want to believe or trust what people tell you and don't even want to invest the time to research it all, why even bother to make the post? Just to be a bother?
                                • 29. Re: Why the ConcurrentModificationException?
                                  Owen Thomas
                                  gimbal2 wrote:
                                  EJP wrote:
                                  The explicit mention of this detail is informative.
                                  The explicit mention of this detail is entirely obvious from the Javadoc. "Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics."
                                  Yes but its written on the internet, that is not to be trusted. Don't you understand that !?!?!?

                                  Why does there have to be a thread like this at least once a week? If you don't want to believe or trust what people tell you and don't even want to invest the time to research it all, why even bother to make the post? Just to be a bother?
                                  I really find this whole ego bluster bullshit you fellas go on with most tiresome. Time and again, I have ignored various attempts to bait me, but I'll take this bait simply because I can.

                                  I enjoy asking questions of others because when others are receptive to the questions I put, I can better expose and discard any prejudice I have. I was asking you a question because I didn't understand what an Iterator is. Even though explicit mention of the detail of the Iterator might be entirely obvious from the javadoc, my misunderstanding might prejudice the meaning I am receiving in what I'm reading. It appears that your answers (minus the bluster) have been helpful, and I feel satisfied that my understanding of a Collection's Iterator has benefited from this exercise.

                                  This is someone else's thread now. I have removed it from my watch list, and although I may take a look at it to see what bickering might ensue, I'll probably distance myself from it.

                                  Until next time.