3

I've this following code chunk using which i want to store the value in JSONObject in order to iterate over it and render it over front end.

        JSONObject dlCovs = jsonObject.getJSONObject("result").getJSONObject("cov_details");
        Iterator x = dlCovs.keys();
        while (x.hasNext()){
            String key1 = (String) x.next();
            String value1 = dlCovs.optString(key1);
            dlCovs.put("covabbrv",key1);
            dlCovs.put("dcIssuedt",value1);
            dlCovs.put("vecatg",key1);
            dlData.put("dlCovs", dlCovs);
        }

       /* dlCovs.put("covabbrv",cov);
        dlCovs.put("issue_date",issue_date);*/
        dlObj.put("status","valid");
        dlData.put("dlCovs", dlCovs);
        dlData.put("status","valid");

while iterating over the data in while loop i'm getting java.util.concurrentModificationException, their is also a case when i'm trying to debug it it is storing the value in dlData.put("dlCovs",dlCovs), but as soon as it's coming for iteration for the second time it throws error. I've following JSON value

 "cov_details": {
     "MCWG": "NA",
     "3WTR": "NA",
     "PSV BUS": "NA",
     "LMV": "NA",
     "INVCRG": "NA"
 },

Any help will be highly appreciable,thanks well in advance enter code here

SujitKumar
  • 142
  • 1
  • 10
Mavericks
  • 283
  • 1
  • 7
  • 20

2 Answers2

8

You're modifying the map whilst iterating its entries. You can't do that.

Put the items into a separate map, then add that to your "main" map afterwards:

  Map newEntries = new HashMap();
  Iterator x = dlCovs.keys();
  while (x.hasNext()){
    String key1 = (String) x.next();
    String value1 = dlCovs.optString(key1);
    newEntries.put("covabbrv",key1);
    // .. etc
  }

  dlCovs.putAll(newEntries);

Also: don't use raw types. You should be declaring x like:

Iterator<String> x = dlCovs.keys();

then you don't need the cast:

String key1 = x.next();

Similarly:

Map<String, String> newEntries = new HashMap<>();
Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Even if i'm putting d val after iterating over the loop i'm getting the same error, chunk of code dlCovs = jsonObject.getJSONObject("result").getJSONObject("cov_details"); Iterator x = dlCovs.keys(); while (x.hasNext()){ String key1 = (String) x.next(); String value1 = dlCovs.optString(key1); dlCovs.put("covabbrv",key1); dlCovs.put("dcIssuedt",value1); dlCovs.put("vecatg",key1); } dlData.put("dlCovs", dlCovs); – Mavericks Jan 03 '17 at 12:59
  • You're still calling `dlCovs.put` inside that loop, whilst iterating over `dlCovs.keys()`. – Andy Turner Jan 03 '17 at 12:59
  • Andy, thanks for Pointing out the mistake, it worked for me. Thanks a lot. – Mavericks Jan 03 '17 at 13:06
0

While iterating over maps using an iterator you cannot change the underlying map. It is called a Fail fast iterator which reads directly from the underlying structure. Java maintains an internal flag called mods which counts the number of structural changes made to the map. If the iterator finds mods to change while it is iterating, then it throws a Concurrent Modification Exception. In Java concurrent hash maps do solve the problem by implementing fail safe iterators. Main thing is you cannot change the structure of map while iterating

Deepayan Ghosh
  • 185
  • 2
  • 9
  • thank you for the explanation, but at the same time now i'm getting a problem which is same as above, now the problem is it's saving only the last value of the Object while iterating over other Lists as mentioned in JSON data,after debugging i get to see that only "INVCRG": "NA" is being stored on dlCovs value – Mavericks Jan 03 '17 at 15:05