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

WebLogic load jar without class file?
Can I scroll a ScrollView programmatically in Android?
Java - CheckBox Listener
Finding the length of a video in Java and ffmpeg
View Changes In Force Portrait Mode
set servletConfig by java
File input output question with Inputstreamreader
JTextField size
Lookup via Annotation in Weblogic
object reference
Is it possible to change the log4j level in my packages but not change the levels in api's im using e.g spring
Java dates problem
Loading Resources from the ClassPath in Java ME
How do I speed up the scroll speed in a JScrollPane when using the mouse wheel?
About Policy Enforcement with AspectJ
Selenium testing and emma code coverage

Categories

HOME
testing
ionic-framework
atom-editor
path-finding
onedrive
analysis
yarn
tizen-web-app
ckeditor
ravendb
flyway4
constraint-programming
node-notifier
applepay
slurm
primary-key
facebook-php-sdk
nano-server
quickbooks
messages
windows-server-2012
google-static-maps
serilog
code-review
karma-jasmine
introduction
swingx
predix
swiftlint
kryo
claims-based-identity
facebook-access-token
virtualdub
linkerd
opentype
saas
javascriptcore
uisplitview
gtrendsr
perlin-noise
usbserial
amazon-kinesis-kpl
ios5
vxworks
bitbucket-pipelines
serve
objectlistview
theano.scan
document.write
webdriver-manager
thin
jquery-nestable
promela
logparser
recursive-datastructures
setter
magma
media-player
memory-alignment
epson
smart-table
eventkit
medium.com
azure-virtual-network
intrusion-detection
mathematica-frontend
storekit
fuzzy-search
persist
sigabrt
nessus
srand
icu4j
tmuxinator
security-testing
twirl
method-parameters
operation
remobjects
p2
inmobi
csquery
rdtsc
wordpress-theme-customize
php-5.4
uitouch
dml
htmlcleaner
poller
floating-point-precision
c18
jsctypes
inbox
spring-io
ora-00911
shellexecute
rabl
mqx
eventlistener
gil
nsnetservice
broken-links
hosts-file
onsubmit
ocx
regsvr32
cufon
netdna-api
mercurial-server
locationlistener
appendto
wise
mirah
asp.net-profiles
firefox-5
avatar

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