Java - Exception in thread “main” java.util.ConcurrentModificationException












0














Is there any way I can modify the HashMap values of a particular key while iterating over it?



A sample program is given below:



public static void main(String args) {
HashMap<Integer,ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>();
ArrayList<String> ar = new ArrayList<String>();
for(int i=0;i<50;i++){
ar.add(Integer.toString(i));
}

hm.put(1, ar);

for(String s:hm.get(1)){
hm.get(1).add("hello");
}

}


Error Thrown:



Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Excp.main(Excp.java:17)









share|improve this question
























  • I don't understand what you are trying to accomplish. The for loop just adds "hello" to the ArrayList for every String in it...couldn't you just add ArrayList.size() number of "hello" strings to that ArrayList? But I have no idea why you would want to do that.
    – Jared
    Oct 17 '14 at 5:23


















0














Is there any way I can modify the HashMap values of a particular key while iterating over it?



A sample program is given below:



public static void main(String args) {
HashMap<Integer,ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>();
ArrayList<String> ar = new ArrayList<String>();
for(int i=0;i<50;i++){
ar.add(Integer.toString(i));
}

hm.put(1, ar);

for(String s:hm.get(1)){
hm.get(1).add("hello");
}

}


Error Thrown:



Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Excp.main(Excp.java:17)









share|improve this question
























  • I don't understand what you are trying to accomplish. The for loop just adds "hello" to the ArrayList for every String in it...couldn't you just add ArrayList.size() number of "hello" strings to that ArrayList? But I have no idea why you would want to do that.
    – Jared
    Oct 17 '14 at 5:23
















0












0








0


1





Is there any way I can modify the HashMap values of a particular key while iterating over it?



A sample program is given below:



public static void main(String args) {
HashMap<Integer,ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>();
ArrayList<String> ar = new ArrayList<String>();
for(int i=0;i<50;i++){
ar.add(Integer.toString(i));
}

hm.put(1, ar);

for(String s:hm.get(1)){
hm.get(1).add("hello");
}

}


Error Thrown:



Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Excp.main(Excp.java:17)









share|improve this question















Is there any way I can modify the HashMap values of a particular key while iterating over it?



A sample program is given below:



public static void main(String args) {
HashMap<Integer,ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>();
ArrayList<String> ar = new ArrayList<String>();
for(int i=0;i<50;i++){
ar.add(Integer.toString(i));
}

hm.put(1, ar);

for(String s:hm.get(1)){
hm.get(1).add("hello");
}

}


Error Thrown:



Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Excp.main(Excp.java:17)






java collections concurrency hashmap concurrentmodification






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 17 '14 at 5:19









rayryeng

82.6k17111139




82.6k17111139










asked Oct 17 '14 at 5:16









RaghuRaghu

1427




1427












  • I don't understand what you are trying to accomplish. The for loop just adds "hello" to the ArrayList for every String in it...couldn't you just add ArrayList.size() number of "hello" strings to that ArrayList? But I have no idea why you would want to do that.
    – Jared
    Oct 17 '14 at 5:23




















  • I don't understand what you are trying to accomplish. The for loop just adds "hello" to the ArrayList for every String in it...couldn't you just add ArrayList.size() number of "hello" strings to that ArrayList? But I have no idea why you would want to do that.
    – Jared
    Oct 17 '14 at 5:23


















I don't understand what you are trying to accomplish. The for loop just adds "hello" to the ArrayList for every String in it...couldn't you just add ArrayList.size() number of "hello" strings to that ArrayList? But I have no idea why you would want to do that.
– Jared
Oct 17 '14 at 5:23






I don't understand what you are trying to accomplish. The for loop just adds "hello" to the ArrayList for every String in it...couldn't you just add ArrayList.size() number of "hello" strings to that ArrayList? But I have no idea why you would want to do that.
– Jared
Oct 17 '14 at 5:23














7 Answers
7






active

oldest

votes


















1














This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.



Below peice of code is causing the problem.



for(String s:hm.get(1)){
hm.get(1).add("hello");
}


You are iterating and modifying the same. Avoid this by creating new ArrayList



  ArrayList<String> ar1 = new ArrayList<String>();

for (String s : hm.get(1)) {
ar1.add("hello");
}


have a read here






share|improve this answer

















  • 1




    I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the ArrayList at hm.get(1) with "hello" strings--which this solution does not do.
    – Jared
    Oct 17 '14 at 7:56





















2














When we are trying to modify collection object while iterating then we get this exception. Check following code:



for(String str : stringList){ 
stringList.add("Test");
}


So in above we get runtime exception.



Solution



Use iterator over For-Each loop, like:



static void filter(Collection<?> c) {
for (Iterator<?> it = c.iterator(); it.hasNext(); )
if (!anyCondition(it.next()))
it.remove();
}


So basic difference between For-Each and iterator is, we can modify collection while iterating using only iterator.






share|improve this answer





























    1














    The problem in the code your presented isn't modifying the HashMap, it's modifying the ArrayList while iterating it.
    You can avoid this exception if you use ar's ListIterator instead of using an enhanced for loop:



    for (ListIterator<String> i = ar.listIterator(); i.hasNext(); i.next()) {
    i.add("hello");
    }





    share|improve this answer





























      0














      If try to modify while iterating your list you will get this Exception.



      for(String s:hm.get(1)){ // iterate 
      hm.get(1).add("hello");//modify
      }


      Both operation affect to hm



      You don't need to iterate here. Just use



      hm.get(1).add("hello");





      share|improve this answer





























        0














        If you want to add to the original ArrayList, then iterate through it on your own:



        final ArrayList<String> arr = hm.get(1);
        final int size = arr.size();

        // this will add size number of "hello" strings to ArrayList arr
        for(int i = 0; i < size; ++i){
        // you don't appear to ever use this value
        final String s = arr.get(i);

        // do something to arr
        arr.add("hello");
        }





        share|improve this answer





























          0














          Although not related to the question, but just adding



          ConcurrentModificationException can also occur if you get the iterator over a collection first and then add some more elements over it and then iterating over the collection will throw this exception.



          For example :



          package got;

          import java.util.*;

          public class GotCharacters {


          public static void main(String... args){

          Person p1 = new Person("TL", 40, "Tyrion Lannister");
          Person p2 = new Person("JM", 50, "Jorah Mormont");
          Person p3 = new Person("AS", 20, "Arya Stark");

          //Defining the collection and adding some elements
          ArrayList<Person> al;
          al = new ArrayList<Person>();
          al.add(p1);
          al.add(p2);
          al.add(p3);

          //Getting the iterator
          Iterator<Person> itr = al.iterator();

          Royalty r1 = new Student("DT", 25, "Daenerys Targaryen", "DragonMother", "Targaryen");
          Royalty r2 = new Student("JS", 28, "Jon Snow", "Lord Commander", "Targaryen");
          Collection<Royalty> c = new ArrayList<Royalty>();
          c.add(s1);
          c.add(s2);

          //Adding more elements after getting the iterator
          al.addAll(c);

          while(itr.hasNext()){
          System.out.print(itr.next());
          }
          }
          }


          Outcome :



          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 myArrayList.ArrayList1.main(ArrayList1.java:34)





          share|improve this answer





















          • Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
            – kunal
            Aug 15 '17 at 9:52





















          0














          Concurrent Modification in programming means to modify an object concurrently when another task is already running over it. Fail Fast And Fail Safe Iterators in Java
          Iterators in java are used to iterate over the Collection objects. Fail-Fast iterators immediately throw ConcurrentModificationException if there is structural modification of the collection. Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators.
          Please use ConcurrentHashMap if you want to modify in between.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26418325%2fjava-exception-in-thread-main-java-util-concurrentmodificationexception%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.



            Below peice of code is causing the problem.



            for(String s:hm.get(1)){
            hm.get(1).add("hello");
            }


            You are iterating and modifying the same. Avoid this by creating new ArrayList



              ArrayList<String> ar1 = new ArrayList<String>();

            for (String s : hm.get(1)) {
            ar1.add("hello");
            }


            have a read here






            share|improve this answer

















            • 1




              I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the ArrayList at hm.get(1) with "hello" strings--which this solution does not do.
              – Jared
              Oct 17 '14 at 7:56


















            1














            This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.



            Below peice of code is causing the problem.



            for(String s:hm.get(1)){
            hm.get(1).add("hello");
            }


            You are iterating and modifying the same. Avoid this by creating new ArrayList



              ArrayList<String> ar1 = new ArrayList<String>();

            for (String s : hm.get(1)) {
            ar1.add("hello");
            }


            have a read here






            share|improve this answer

















            • 1




              I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the ArrayList at hm.get(1) with "hello" strings--which this solution does not do.
              – Jared
              Oct 17 '14 at 7:56
















            1












            1








            1






            This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.



            Below peice of code is causing the problem.



            for(String s:hm.get(1)){
            hm.get(1).add("hello");
            }


            You are iterating and modifying the same. Avoid this by creating new ArrayList



              ArrayList<String> ar1 = new ArrayList<String>();

            for (String s : hm.get(1)) {
            ar1.add("hello");
            }


            have a read here






            share|improve this answer












            This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.



            Below peice of code is causing the problem.



            for(String s:hm.get(1)){
            hm.get(1).add("hello");
            }


            You are iterating and modifying the same. Avoid this by creating new ArrayList



              ArrayList<String> ar1 = new ArrayList<String>();

            for (String s : hm.get(1)) {
            ar1.add("hello");
            }


            have a read here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 17 '14 at 5:22









            Ankur SinghalAnkur Singhal

            17.8k44984




            17.8k44984








            • 1




              I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the ArrayList at hm.get(1) with "hello" strings--which this solution does not do.
              – Jared
              Oct 17 '14 at 7:56
















            • 1




              I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the ArrayList at hm.get(1) with "hello" strings--which this solution does not do.
              – Jared
              Oct 17 '14 at 7:56










            1




            1




            I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the ArrayList at hm.get(1) with "hello" strings--which this solution does not do.
            – Jared
            Oct 17 '14 at 7:56






            I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the ArrayList at hm.get(1) with "hello" strings--which this solution does not do.
            – Jared
            Oct 17 '14 at 7:56















            2














            When we are trying to modify collection object while iterating then we get this exception. Check following code:



            for(String str : stringList){ 
            stringList.add("Test");
            }


            So in above we get runtime exception.



            Solution



            Use iterator over For-Each loop, like:



            static void filter(Collection<?> c) {
            for (Iterator<?> it = c.iterator(); it.hasNext(); )
            if (!anyCondition(it.next()))
            it.remove();
            }


            So basic difference between For-Each and iterator is, we can modify collection while iterating using only iterator.






            share|improve this answer


























              2














              When we are trying to modify collection object while iterating then we get this exception. Check following code:



              for(String str : stringList){ 
              stringList.add("Test");
              }


              So in above we get runtime exception.



              Solution



              Use iterator over For-Each loop, like:



              static void filter(Collection<?> c) {
              for (Iterator<?> it = c.iterator(); it.hasNext(); )
              if (!anyCondition(it.next()))
              it.remove();
              }


              So basic difference between For-Each and iterator is, we can modify collection while iterating using only iterator.






              share|improve this answer
























                2












                2








                2






                When we are trying to modify collection object while iterating then we get this exception. Check following code:



                for(String str : stringList){ 
                stringList.add("Test");
                }


                So in above we get runtime exception.



                Solution



                Use iterator over For-Each loop, like:



                static void filter(Collection<?> c) {
                for (Iterator<?> it = c.iterator(); it.hasNext(); )
                if (!anyCondition(it.next()))
                it.remove();
                }


                So basic difference between For-Each and iterator is, we can modify collection while iterating using only iterator.






                share|improve this answer












                When we are trying to modify collection object while iterating then we get this exception. Check following code:



                for(String str : stringList){ 
                stringList.add("Test");
                }


                So in above we get runtime exception.



                Solution



                Use iterator over For-Each loop, like:



                static void filter(Collection<?> c) {
                for (Iterator<?> it = c.iterator(); it.hasNext(); )
                if (!anyCondition(it.next()))
                it.remove();
                }


                So basic difference between For-Each and iterator is, we can modify collection while iterating using only iterator.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 8 '16 at 11:04









                Mayur GiteMayur Gite

                669




                669























                    1














                    The problem in the code your presented isn't modifying the HashMap, it's modifying the ArrayList while iterating it.
                    You can avoid this exception if you use ar's ListIterator instead of using an enhanced for loop:



                    for (ListIterator<String> i = ar.listIterator(); i.hasNext(); i.next()) {
                    i.add("hello");
                    }





                    share|improve this answer


























                      1














                      The problem in the code your presented isn't modifying the HashMap, it's modifying the ArrayList while iterating it.
                      You can avoid this exception if you use ar's ListIterator instead of using an enhanced for loop:



                      for (ListIterator<String> i = ar.listIterator(); i.hasNext(); i.next()) {
                      i.add("hello");
                      }





                      share|improve this answer
























                        1












                        1








                        1






                        The problem in the code your presented isn't modifying the HashMap, it's modifying the ArrayList while iterating it.
                        You can avoid this exception if you use ar's ListIterator instead of using an enhanced for loop:



                        for (ListIterator<String> i = ar.listIterator(); i.hasNext(); i.next()) {
                        i.add("hello");
                        }





                        share|improve this answer












                        The problem in the code your presented isn't modifying the HashMap, it's modifying the ArrayList while iterating it.
                        You can avoid this exception if you use ar's ListIterator instead of using an enhanced for loop:



                        for (ListIterator<String> i = ar.listIterator(); i.hasNext(); i.next()) {
                        i.add("hello");
                        }






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 17 '14 at 5:22









                        MureinikMureinik

                        180k22130199




                        180k22130199























                            0














                            If try to modify while iterating your list you will get this Exception.



                            for(String s:hm.get(1)){ // iterate 
                            hm.get(1).add("hello");//modify
                            }


                            Both operation affect to hm



                            You don't need to iterate here. Just use



                            hm.get(1).add("hello");





                            share|improve this answer


























                              0














                              If try to modify while iterating your list you will get this Exception.



                              for(String s:hm.get(1)){ // iterate 
                              hm.get(1).add("hello");//modify
                              }


                              Both operation affect to hm



                              You don't need to iterate here. Just use



                              hm.get(1).add("hello");





                              share|improve this answer
























                                0












                                0








                                0






                                If try to modify while iterating your list you will get this Exception.



                                for(String s:hm.get(1)){ // iterate 
                                hm.get(1).add("hello");//modify
                                }


                                Both operation affect to hm



                                You don't need to iterate here. Just use



                                hm.get(1).add("hello");





                                share|improve this answer












                                If try to modify while iterating your list you will get this Exception.



                                for(String s:hm.get(1)){ // iterate 
                                hm.get(1).add("hello");//modify
                                }


                                Both operation affect to hm



                                You don't need to iterate here. Just use



                                hm.get(1).add("hello");






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 17 '14 at 5:22









                                Ruchira Gayan RanaweeraRuchira Gayan Ranaweera

                                25.7k145897




                                25.7k145897























                                    0














                                    If you want to add to the original ArrayList, then iterate through it on your own:



                                    final ArrayList<String> arr = hm.get(1);
                                    final int size = arr.size();

                                    // this will add size number of "hello" strings to ArrayList arr
                                    for(int i = 0; i < size; ++i){
                                    // you don't appear to ever use this value
                                    final String s = arr.get(i);

                                    // do something to arr
                                    arr.add("hello");
                                    }





                                    share|improve this answer


























                                      0














                                      If you want to add to the original ArrayList, then iterate through it on your own:



                                      final ArrayList<String> arr = hm.get(1);
                                      final int size = arr.size();

                                      // this will add size number of "hello" strings to ArrayList arr
                                      for(int i = 0; i < size; ++i){
                                      // you don't appear to ever use this value
                                      final String s = arr.get(i);

                                      // do something to arr
                                      arr.add("hello");
                                      }





                                      share|improve this answer
























                                        0












                                        0








                                        0






                                        If you want to add to the original ArrayList, then iterate through it on your own:



                                        final ArrayList<String> arr = hm.get(1);
                                        final int size = arr.size();

                                        // this will add size number of "hello" strings to ArrayList arr
                                        for(int i = 0; i < size; ++i){
                                        // you don't appear to ever use this value
                                        final String s = arr.get(i);

                                        // do something to arr
                                        arr.add("hello");
                                        }





                                        share|improve this answer












                                        If you want to add to the original ArrayList, then iterate through it on your own:



                                        final ArrayList<String> arr = hm.get(1);
                                        final int size = arr.size();

                                        // this will add size number of "hello" strings to ArrayList arr
                                        for(int i = 0; i < size; ++i){
                                        // you don't appear to ever use this value
                                        final String s = arr.get(i);

                                        // do something to arr
                                        arr.add("hello");
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Oct 17 '14 at 5:25









                                        JaredJared

                                        88658




                                        88658























                                            0














                                            Although not related to the question, but just adding



                                            ConcurrentModificationException can also occur if you get the iterator over a collection first and then add some more elements over it and then iterating over the collection will throw this exception.



                                            For example :



                                            package got;

                                            import java.util.*;

                                            public class GotCharacters {


                                            public static void main(String... args){

                                            Person p1 = new Person("TL", 40, "Tyrion Lannister");
                                            Person p2 = new Person("JM", 50, "Jorah Mormont");
                                            Person p3 = new Person("AS", 20, "Arya Stark");

                                            //Defining the collection and adding some elements
                                            ArrayList<Person> al;
                                            al = new ArrayList<Person>();
                                            al.add(p1);
                                            al.add(p2);
                                            al.add(p3);

                                            //Getting the iterator
                                            Iterator<Person> itr = al.iterator();

                                            Royalty r1 = new Student("DT", 25, "Daenerys Targaryen", "DragonMother", "Targaryen");
                                            Royalty r2 = new Student("JS", 28, "Jon Snow", "Lord Commander", "Targaryen");
                                            Collection<Royalty> c = new ArrayList<Royalty>();
                                            c.add(s1);
                                            c.add(s2);

                                            //Adding more elements after getting the iterator
                                            al.addAll(c);

                                            while(itr.hasNext()){
                                            System.out.print(itr.next());
                                            }
                                            }
                                            }


                                            Outcome :



                                            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 myArrayList.ArrayList1.main(ArrayList1.java:34)





                                            share|improve this answer





















                                            • Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
                                              – kunal
                                              Aug 15 '17 at 9:52


















                                            0














                                            Although not related to the question, but just adding



                                            ConcurrentModificationException can also occur if you get the iterator over a collection first and then add some more elements over it and then iterating over the collection will throw this exception.



                                            For example :



                                            package got;

                                            import java.util.*;

                                            public class GotCharacters {


                                            public static void main(String... args){

                                            Person p1 = new Person("TL", 40, "Tyrion Lannister");
                                            Person p2 = new Person("JM", 50, "Jorah Mormont");
                                            Person p3 = new Person("AS", 20, "Arya Stark");

                                            //Defining the collection and adding some elements
                                            ArrayList<Person> al;
                                            al = new ArrayList<Person>();
                                            al.add(p1);
                                            al.add(p2);
                                            al.add(p3);

                                            //Getting the iterator
                                            Iterator<Person> itr = al.iterator();

                                            Royalty r1 = new Student("DT", 25, "Daenerys Targaryen", "DragonMother", "Targaryen");
                                            Royalty r2 = new Student("JS", 28, "Jon Snow", "Lord Commander", "Targaryen");
                                            Collection<Royalty> c = new ArrayList<Royalty>();
                                            c.add(s1);
                                            c.add(s2);

                                            //Adding more elements after getting the iterator
                                            al.addAll(c);

                                            while(itr.hasNext()){
                                            System.out.print(itr.next());
                                            }
                                            }
                                            }


                                            Outcome :



                                            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 myArrayList.ArrayList1.main(ArrayList1.java:34)





                                            share|improve this answer





















                                            • Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
                                              – kunal
                                              Aug 15 '17 at 9:52
















                                            0












                                            0








                                            0






                                            Although not related to the question, but just adding



                                            ConcurrentModificationException can also occur if you get the iterator over a collection first and then add some more elements over it and then iterating over the collection will throw this exception.



                                            For example :



                                            package got;

                                            import java.util.*;

                                            public class GotCharacters {


                                            public static void main(String... args){

                                            Person p1 = new Person("TL", 40, "Tyrion Lannister");
                                            Person p2 = new Person("JM", 50, "Jorah Mormont");
                                            Person p3 = new Person("AS", 20, "Arya Stark");

                                            //Defining the collection and adding some elements
                                            ArrayList<Person> al;
                                            al = new ArrayList<Person>();
                                            al.add(p1);
                                            al.add(p2);
                                            al.add(p3);

                                            //Getting the iterator
                                            Iterator<Person> itr = al.iterator();

                                            Royalty r1 = new Student("DT", 25, "Daenerys Targaryen", "DragonMother", "Targaryen");
                                            Royalty r2 = new Student("JS", 28, "Jon Snow", "Lord Commander", "Targaryen");
                                            Collection<Royalty> c = new ArrayList<Royalty>();
                                            c.add(s1);
                                            c.add(s2);

                                            //Adding more elements after getting the iterator
                                            al.addAll(c);

                                            while(itr.hasNext()){
                                            System.out.print(itr.next());
                                            }
                                            }
                                            }


                                            Outcome :



                                            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 myArrayList.ArrayList1.main(ArrayList1.java:34)





                                            share|improve this answer












                                            Although not related to the question, but just adding



                                            ConcurrentModificationException can also occur if you get the iterator over a collection first and then add some more elements over it and then iterating over the collection will throw this exception.



                                            For example :



                                            package got;

                                            import java.util.*;

                                            public class GotCharacters {


                                            public static void main(String... args){

                                            Person p1 = new Person("TL", 40, "Tyrion Lannister");
                                            Person p2 = new Person("JM", 50, "Jorah Mormont");
                                            Person p3 = new Person("AS", 20, "Arya Stark");

                                            //Defining the collection and adding some elements
                                            ArrayList<Person> al;
                                            al = new ArrayList<Person>();
                                            al.add(p1);
                                            al.add(p2);
                                            al.add(p3);

                                            //Getting the iterator
                                            Iterator<Person> itr = al.iterator();

                                            Royalty r1 = new Student("DT", 25, "Daenerys Targaryen", "DragonMother", "Targaryen");
                                            Royalty r2 = new Student("JS", 28, "Jon Snow", "Lord Commander", "Targaryen");
                                            Collection<Royalty> c = new ArrayList<Royalty>();
                                            c.add(s1);
                                            c.add(s2);

                                            //Adding more elements after getting the iterator
                                            al.addAll(c);

                                            while(itr.hasNext()){
                                            System.out.print(itr.next());
                                            }
                                            }
                                            }


                                            Outcome :



                                            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 myArrayList.ArrayList1.main(ArrayList1.java:34)






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Aug 15 '17 at 7:38









                                            kunalkunal

                                            507




                                            507












                                            • Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
                                              – kunal
                                              Aug 15 '17 at 9:52




















                                            • Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
                                              – kunal
                                              Aug 15 '17 at 9:52


















                                            Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
                                            – kunal
                                            Aug 15 '17 at 9:52






                                            Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
                                            – kunal
                                            Aug 15 '17 at 9:52













                                            0














                                            Concurrent Modification in programming means to modify an object concurrently when another task is already running over it. Fail Fast And Fail Safe Iterators in Java
                                            Iterators in java are used to iterate over the Collection objects. Fail-Fast iterators immediately throw ConcurrentModificationException if there is structural modification of the collection. Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators.
                                            Please use ConcurrentHashMap if you want to modify in between.






                                            share|improve this answer


























                                              0














                                              Concurrent Modification in programming means to modify an object concurrently when another task is already running over it. Fail Fast And Fail Safe Iterators in Java
                                              Iterators in java are used to iterate over the Collection objects. Fail-Fast iterators immediately throw ConcurrentModificationException if there is structural modification of the collection. Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators.
                                              Please use ConcurrentHashMap if you want to modify in between.






                                              share|improve this answer
























                                                0












                                                0








                                                0






                                                Concurrent Modification in programming means to modify an object concurrently when another task is already running over it. Fail Fast And Fail Safe Iterators in Java
                                                Iterators in java are used to iterate over the Collection objects. Fail-Fast iterators immediately throw ConcurrentModificationException if there is structural modification of the collection. Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators.
                                                Please use ConcurrentHashMap if you want to modify in between.






                                                share|improve this answer












                                                Concurrent Modification in programming means to modify an object concurrently when another task is already running over it. Fail Fast And Fail Safe Iterators in Java
                                                Iterators in java are used to iterate over the Collection objects. Fail-Fast iterators immediately throw ConcurrentModificationException if there is structural modification of the collection. Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators.
                                                Please use ConcurrentHashMap if you want to modify in between.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 13 '18 at 7:34









                                                harshith hmharshith hm

                                                294




                                                294






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26418325%2fjava-exception-in-thread-main-java-util-concurrentmodificationexception%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Xamarin.iOS Cant Deploy on Iphone

                                                    Glorious Revolution

                                                    Dulmage-Mendelsohn matrix decomposition in Python