title: How to fix ConcurrentModificationException when using foreach to iterate over a List?
body: <p>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.</p>
  
  <p>Any help?</p>
  
  <p>here is the code that throws the exception:</p>
  
  <pre><code>List&lt;ContexteNb&gt; projets = service.findByprojet(p);
          List&lt;ProjetVTO&gt; models = new ArrayList&lt;&gt;();
          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())
                          &amp;&amp; (projetModel.getPriorite().equals(model.getPriorite()))) {
                      projetModel.setNbillets(projetModel.getNbillets() + model.getNbillets());
  
                  } else {
                      models.add(model);
                  }}}}
  </code></pre>
  
  <p>thanks,</p>