java.util.ConcurrentModificationException in Android : <p>I am writing a piece of code which displays in an expandable list using information from an AsyncTask and a pre defined HashMap. But it is throwing <strong>java.util.ConcurrentModificationException</strong>. My code is as follows:</p>

<p>AsnycTask</p>

<pre><code>private class BackTask extends AsyncTask&lt;Void, Void, Void&gt; {
        ProgressDialog pd;
        ArrayList&lt;String&gt; name;
        ArrayList&lt;Integer&gt; quantity;
        Map&lt;String, Map&lt;String, Integer&gt;&gt; cart_names1 = new HashMap&lt;String, Map&lt;String, Integer&gt;&gt;();

        public BackTask(ArrayList&lt;String&gt; name, ArrayList&lt;Integer&gt; quantity) {
            this.name = name;
            this.quantity = quantity;
        }

        protected void onPreExecute() {

            super.onPreExecute();

            pd = new ProgressDialog(ha);

            pd.setTitle("Retrieving data");

            pd.setMessage("Please wait.");

            pd.setCancelable(true);

            pd.setIndeterminate(true);

            pd.show();


        }

        protected Void doInBackground(Void... arg0) {
            InputStream is = null;
            String result = "";
            try {
                String link = "http://chutte.co.nf/get_item_prices.php?";
                for (int b = 0; b &lt; name.size(); b++) {
                    link += "names[]" + "=" + name.get(b) + "&amp;";

                }
                for (int a = 0; a &lt; quantity.size(); a++) {
                    link += "quantities[]" + "=" + quantity.get(a);
                    if (a != quantity.size() - 1) {
                        link += "&amp;";
                    }
                }
                Log.e("ERROR", link);
                URL url = new URL(link);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                is = urlConnection.getInputStream();
            } catch (Exception e) {
                if (pd != null) pd.dismiss();


                Log.e("ERROR", e.getMessage());
            }
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder builder = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    builder.append(line + "\n");

                }
                is.close();
                result = builder.toString();
            } catch (Exception e) {
                Log.e("ERROR", "Error converting result " + e.toString());
            }
            try {
                result = result.substring(result.indexOf("["));
                JSONArray jsonArray = new JSONArray(result);
                for (int i = 0; i &lt; jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Map&lt;String, Integer&gt; temmap = new HashMap&lt;&gt;();
                    String temname = jsonObject.getString("Name");
                    temmap.put("First", jsonObject.getInt("First"));
                    temmap.put("Second", jsonObject.getInt("Second"));
                    temmap.put("Third", jsonObject.getInt("Third"));
                    Log.e("ERROR", temmap.get("First").toString());
                    cart_names1.put(temname, temmap);

                }
                strhold2.clear();
                strhold2.add("First");
                strhold2.add("Second");
                strhold2.add("Third");


                String[] strhold1 = new String[strhold2.size()];
                for (int i56 = 0; i56 &lt; strhold2.size(); i56++) {
                    strhold1[i56] = strhold2.get(i56);
                }
                System.out.println(cart_names1);
                Log.e("ERROR", Integer.toString(cart_names1.size()) + "IN LATEST");
                if (cart_names1.size() &gt; 1) {
                    System.out.println(cart_names1.size());
                    System.out.println(strhold2.size());
                    Combination.printCombination(cart_names1, strhold1, strhold2.size(), cart_names1.size(), 2);
                    ArrayList&lt;String&gt; wrong = Permutation.getlist();
                    System.out.println(wrong + "this is final");
                    setalldata(wrong);
                    System.out.println(wrong);
                    couldthis.clear();
                    couldthis.addAll(wrong);
                } else {
                    Single_Permutation.getpermute(cart_names1);
                    System.out.println(Single_Permutation.singlelist);
                    couldthis.clear();
                    couldthis.addAll(Single_Permutation.singlelist);
                    setalldata(Single_Permutation.singlelist);
                    Log.e("ERROR", "thisis the list 1" + getdataformap());
                }

            } catch (Exception e) {
                Log.e("ERROR", "Error pasting data " + e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            /*SetMap setMap = new SetMap(getdataformap());
            setMap.execute();*/
            if (pd != null){ pd.dismiss();}
               ListFragment.addtolist(getdataformap());
                Log.e("ERROR", "This is putmap i" + getdataformap());
                SetMap setMap =new SetMap(getdataformap());
                setMap.execute();
            }
        }

    }
</code></pre>

<p>Fragment(with expandable list)</p>

<pre><code>public static class ListFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static ExpandableListView expandablelistview;
        public static CustomExpandableListAdapter expandableadapter;
        public static HashMap&lt;Fitems, List&lt;Fnitems&gt;&gt; datapforput = new HashMap&lt;&gt;();
        public static List&lt;Fitems&gt; mainforput = new ArrayList&lt;&gt;();
        public static View view;
        public static Context getha;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            view = inflater.inflate(R.layout.activity_listfragment, container, false);
            //doddata();
            expandablelistview = (ExpandableListView) view.findViewById(R.id.expandableListView);
            expandableadapter = new CustomExpandableListAdapter(((Result) getActivity()).getha(), mainforput, datapforput);
            expandablelistview.setAdapter(expandableadapter);
            getha = ((Result) getActivity()).getha();
            return view;
        }

        @Override
        public void onStart() {
            super.onStart();
            Permutation.finallist = new ArrayList&lt;&gt;();
            Single_Permutation.singlelist = new ArrayList&lt;&gt;();
   /* doddata();
    expandablelistview = (ExpandableListView) view.findViewById(R.id.expandableListView);
    expandableadapter = new CustomExpandableListAdapter(((Result)getActivity()).getha(),mainforput,datapforput);
    expandablelistview.setAdapter(expandableadapter);*/
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static void doddata() {
            Fitems fitems1 = new Fitems();
            Fitems fitems2 = new Fitems();
            Fnitems fnitems1 = new Fnitems();
            Fnitems fnitems2 = new Fnitems();
            Fnitems fnitems3 = new Fnitems();
            Fnitems fnitems4 = new Fnitems();
            fitems1.setName("AAA");
            fitems2.setName("BBB");
            fnitems1.setName("AAAa");
            fnitems2.setName("AAAb");
            fnitems3.setName("BBBa");
            fnitems4.setName("BBBb");
            List&lt;Fnitems&gt; listfnitem1 = new ArrayList&lt;&gt;();
            List&lt;Fnitems&gt; listfnitem2 = new ArrayList&lt;&gt;();
            listfnitem1.add(fnitems1);
            listfnitem1.add(fnitems2);
            listfnitem2.add(fnitems3);
            listfnitem2.add(fnitems4);
            datapforput.put(fitems1, listfnitem1);
            datapforput.put(fitems2, listfnitem2);
            mainforput.add(fitems1);
            mainforput.add(fitems2);
            //Log.e("ERROR", "thisis the list for god's sake " + Result.couldthis+datapforput.toString());
            if (Result.couldthis.size() &gt; 0) {
                for (int i = 0; i &lt; (Result.couldthis).size(); i++) {
                    for (Map.Entry&lt;Fitems, List&lt;Fnitems&gt;&gt; entry : datapforput.entrySet()) {
                        if (Result.couldthis.get(i).equals(entry.getKey().getName())){
                            Fnitems fnitems5 = new Fnitems();
                            fnitems5.setName(Search_multiple.cart_records.get(i).getName());
                            entry.getValue().add(fnitems5);
                        }else {
                            Fitems fitems3 = new Fitems();
                            fitems3.setName(Result.couldthis.get(i));
                            Fnitems fnitems5 = new Fnitems();
                            fnitems5.setName(Search_multiple.cart_records.get(i).getName());
                            List&lt;Fnitems&gt; listfnitem3 = new ArrayList&lt;&gt;();
                            listfnitem3.add(fnitems5);
                            datapforput.put(fitems3, listfnitem3);
                            mainforput.add(fitems3);
                        }
                    }
                }
            }
        }

        public static ListFragment newInstance() {
            ListFragment fragment = new ListFragment();
            Log.e("ERROR", "man .... " + fragment.getTag());
            return fragment;
        }

        public static void addtolist(ArrayList&lt;String&gt; dataforputting) {
           // Log.e("ERROR", "thisis the list 3" + (dataforputting));
            //if (expandableadapter != null){
            expandableadapter.clear();//}
            //Log.e("INFO", "This is mainforput" + mainforput + "This is dataforput" + datapforput);
            doddata();
            //Log.e("INFO", "This is mainforput" + mainforput + "This is dataforput" + datapforput);
            expandableadapter = new CustomExpandableListAdapter(getha, mainforput, datapforput);
            expandablelistview.setAdapter(expandableadapter);
        }


    }
</code></pre>

<p>Stack</p>

<pre><code>07-13 18:16:11.955 17339-17339/nf.co.riaah.chutte E/ERROR: Inside populate Second
07-13 18:16:11.986 17339-17339/nf.co.riaah.chutte E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: nf.co.riaah.chutte, PID: 17339
                                                                    java.util.ConcurrentModificationException
                                                                        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:787)
                                                                        at java.util.HashMap$EntryIterator.next(HashMap.java:824)
                                                                        at java.util.HashMap$EntryIterator.next(HashMap.java:822)
                                                                        at nf.co.riaah.chutte.Result$ListFragment.doddata(Result.java:199)
                                                                        at nf.co.riaah.chutte.Result$ListFragment.addtolist(Result.java:230)
                                                                        at nf.co.riaah.chutte.Result$SetMap.onPostExecute(Result.java:1049)
                                                                        at nf.co.riaah.chutte.Result$SetMap.onPostExecute(Result.java:967)
                                                                        at android.os.AsyncTask.finish(AsyncTask.java:636)
                                                                        at android.os.AsyncTask.access$500(AsyncTask.java:177)
                                                                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:139)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5298)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
</code></pre>
