java


How to fix ConcurrentModificationException when using foreach to iterate over a List? [duplicate]


This question already has an answer here:
Java - adding elements to list while iterating over it
5 answers
am trying to add objects to an arraylist after verifying either that object already exits in the list or not. But am getting a ConcurrentModificationException and I don't know how to fix it.
Any help?
here is the code that throws the exception:
List<ContexteNb> projets = service.findByprojet(p);
List<ProjetVTO> models = new ArrayList<>();
for (ContexteNb contexteNb : projets) {
ProjetVTO model = new ProjetVTO();
model.setNbillets(contexteNb.getNbBillet());
model.setAnnee(contexteNb.getDimDate().getAnnee());
model.setPriorite(contexteNb.getDimPriorite().getPriorite());
if (models.isEmpty()) {
models.add(model);
}
else{
for (ProjetVTO projetModel : models) {
if ((projetModel.getAnnee() == model.getAnnee())
&& (projetModel.getPriorite().equals(model.getPriorite()))) {
projetModel.setNbillets(projetModel.getNbillets() + model.getNbillets());
} else {
models.add(model);
}}}}
thanks,
The exception results from adding an element to the models List while iterating over it.
You have to change your logic. I suspect the logic of your inner loop is wrong anyway, and fixing it will also solve your problem. You probably want to search first if the List contains any element matching model and modify it if found, and only add a new instance to the List if you don't find a match (i.e. after the loop is over).
Your inner loop would look like this:
if (models.isEmpty()) {
models.add(model);
} else {
boolean found = false;
for (ProjetVTO projetModel : models) {
if ((projetModel.getAnnee() == model.getAnnee()) && (projetModel.getPriorite().equals(model.getPriorite()))) {
projetModel.setNbillets(projetModel.getNbillets() + model.getNbillets());
found = true;
break;
}
}
if (!found) {
models.add(model);
}
}
or simply (you can eliminate the outer condition):
boolean found = false;
for (ProjetVTO projetModel : models) {
if ((projetModel.getAnnee() == model.getAnnee()) && (projetModel.getPriorite().equals(model.getPriorite()))) {
projetModel.setNbillets(projetModel.getNbillets() + model.getNbillets());
found = true;
break;
}
}
if (!found) {
models.add(model);
}

Related Links

JAVA Comparing two Strings isn't working
Binary Power Error
com.apple.eawt double clicking [duplicate]
Java http server wont receive more then once
What is the command for Selenium Driver to click on the green check on the android keyboard for automated testing?
Find whether each character in 1 string is exist in another string, faster than O(n^2)
What is the theory behind an askpass on Mac?
Managing package dependencies
I can't run the program it says “<terminated> satorreProgram2main(1)[Java Application]”
Passing Date time from Java as mentioned below?
How to get OkHttp3 redirected URL?
Animation sequence in JFrame
httpcomponenets givng error in WAS
java.lang.IllegalAccessError: tried to access method net.sourceforge.tess4j.Tesseract.<init>()V from class Tess4jTest.TestTess
Shape with direct edit blinks in upper left corner of a diagram before layout
Android Background service closes itself instantly when starting it

Categories

HOME
yii2
ionic-framework
blogger
gerrit
view
onedrive
bpmn
react-router
razor
react-redux
cross-browser
mvvmcross
yum
podio
medical
here-api
imacros
circuit
percona
tostring
pc
solaris-10
evopdf
ab-testing
predix
tokenize
captiveportal
format-specifiers
crosstab
pepper
applozic
nameservers
data-manipulation
contact-form
gtrendsr
webtest
windows-dev-center
repo
android-kernel
atl
mpmediaquery
ios5
io-redirection
ssjs
uft-api
android-tabhost
mapzen
sas-jmp
taffy
outlook-2013
alphabet
statsd
nsarray
wdf
static-ip-address
asp.net-4.5
django-debug-toolbar
fuzzy-search
apachebench
natvis
sdhc
sframe
ibaction
createprocessasuser
vstest.console.exe
cisco-ios
dia
modalpopup
tablelayout
applescript-objc
openexr
mysql-error-1062
imdbpy
rabl
enterprisedb
drools-planner
wsdl-2.0
bitsharp
nsobject
external-accessory
coercion
tomcat-valve
xtype
locationlistener
netbeans-7.1
appendto
imac
gamequery
gin
iweb
phonon
anti-piracy
zend-decorators
scripting-languages
zune
w3wp
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App