java.util.ConcurrentModificationException
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Meha_Grover
Posted On:   Friday, April 13, 2007 02:53 PM

Hi, I am trying to run the following code snippet but getting the java.util.ConcurrentModificationException exception. Please advice: public void sort(int columnIndex) { Map indexedData = this.indexDataByColumn(columnIndex); List columnValues = new ArrayList(indexedData.keySet()); List columnTemp=null; for (Iterator iter = columnValues.iterator(); iter.hasNext();) { String sortStr = iter.next().toString(); if (sortStr.startsWith("(")) { sortStr = sortStr.substring(1, sortStr.length() - 1); sortStr = splitString(sortStr, ",").toString(); columnTemp.add(sortStr); } else {   More>>

Hi,

I am trying to run the following code snippet but getting the java.util.ConcurrentModificationException exception. Please advice:



public void sort(int columnIndex) {


Map indexedData = this.indexDataByColumn(columnIndex);

List columnValues = new ArrayList(indexedData.keySet());

List columnTemp=null;

for (Iterator iter = columnValues.iterator();
iter.hasNext();) {

String sortStr = iter.next().toString();

if (sortStr.startsWith("(")) {

sortStr = sortStr.substring(1, sortStr.length() - 1);

sortStr = splitString(sortStr, ",").toString();

columnTemp.add(sortStr);

} else {

sortStr = splitString(sortStr, ",").toString();

columnTemp.add(sortStr);

}

}

columnValues.add(columnTemp);

Collections.sort(columnValues);

this.organizeRows(columnValues, indexedData);



}


private String[] splitString(String str, String delims) {

if (str == null)

return null;

else if (str.equals("") || delims == null || delims.length() == 0)

return new String[]{str};


String[] s;

Vector v = new

Vector ();


int pos = 0;

int newpos = str.indexOf(delims, pos);

while (newpos != -1) {

v.addElement(str.substring(pos, newpos));

pos = newpos + delims.length();

newpos = str.indexOf(delims, pos);

}

v.addElement(str.substring(pos));


s = new String[v.size()];

for (int i = 0, cnt = s.length; i < cnt; i++)

s[i] = v.elementAt(i);


return s;

}


private void organizeRows(List columnValues, Map indexedData) {

List sortedData = new ArrayList(this.getData().size());


for (Iterator iter = columnValues.iterator(); iter.hasNext();) {

Object key = iter.next();

sortedData.addAll((List) indexedData.get(key));

}


this.setData(sortedData);

}


private Map indexDataByColumn(int columnIndex) {

Map indexedData = new MultiValueMap();


for (Iterator iter = this.getData().iterator(); iter.hasNext();) {

Object rowObject = iter.next();


Object[] columnData = this.transformDomainObjectForDisplay(rowObject);

indexedData.put(columnData[columnIndex], rowObject);

}

return indexedData;

}



Thanks,

Meha

   <<Less

Re: java.util.ConcurrentModificationException

Posted By:   Anonymous  
Posted On:   Friday, May 4, 2007 12:26 AM

HI,

This exception occures when one thread is trying to iterate throught the collection and other trying to modify the values in the same collection.
Try copying the values to other collection and then modify them or make the methods which modify the values and method which iterates through the collection as synchronized.

Hope its helpful.

Regards,
Rakesh
About | Sitemap | Contact