ConcurrentModificationException in my own code
<hr>

<p><strong>I think my solution has been incorrectly mistaken with the solution of another question. It has also been edited so it looks like the solution is the one pointed by the duplicate, but this obviously did not solve my problem, just avoided the exception raised (the surface of the problem). Since my efforts to reopen the question have been useless, I have opened a new one <a href="https://stackoverflow.com/questions/40442340/jsf-wrong-display-of-elements-when-iterating-a-map">here</a>.</strong></p>

<hr>

<p>I am getting a <code>ConcurrentModificationException</code> and I am not able to solve this problem. So I remove a series of elements from a list and I get an error.</p>

<p>This is the error:</p>

<pre><code>04-Nov-2016 15:49:50.488 SEVERE [http-nio-8080-exec-199] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Faces Servlet] in context with path [/...] threw exception [java.util.ConcurrentModificationException] with root cause
 java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886)
    at java.util.ArrayList$Itr.next(ArrayList.java:836)
    at com.ex.PersonController.removeGroup(...)
    at ....
</code></pre>

<p>The Java code:</p>

<pre><code>public void removeGroup(int age) {
    if (person != null) {
        List&lt;Person&gt; friends = person.getFriends();
        List&lt;Person&gt; removeFriends = new ArrayList&lt;&gt;();
        if (friends != null) {
            for (Person p : friends) {
                if (p.getAge() == age) {
                    removeFriends.add(p);
                } 
            }
            friends.removeAll(removeFriends);
        }
    }
</code></pre>

<hr>

<p><strong>EDIT:</strong>
Note that <a href="http://Iterating%20through%20a%20Collection,%20avoiding%20ConcurrentModificationException%20when%20removing%20in%20loop" rel="nofollow noreferrer">this</a> apparently is not solving my problem. It avoids raising an error, though, but the results displayed by JSF are not correct. Is it possible that JSF is trying to refresh while my list is not fully updated?</p>

<hr>

<p><strong>EDIT2:</strong>
I don't think this is a duplicate. The error I got with some of my tests is gone with <a href="http://Iterating%20through%20a%20Collection,%20avoiding%20ConcurrentModificationException%20when%20removing%20in%20loop" rel="nofollow noreferrer">this</a> or <a href="https://stackoverflow.com/questions/8104692/how-to-avoid-java-util-concurrentmodificationexception-when-iterating-through-an">this</a> (note that even with the .add() approach I got the same error for some reason). But JSF is still not printing what I expect. I write again the code that BalusC removed from my question with a bit more of information:</p>

<p>The xhtml Java code:</p>

<pre><code>&lt;c:forEach items="#{personController.groupedPersons}" var="personG"&gt;
    &lt;h:commandButton action="#{personController.removeGroup(personG.key)}" type="submit" value="Remove" &gt;
        &lt;f:ajax render="@form" /&gt;
    &lt;/h:commandButton&gt;
    #{personG.key} - #{personG.value.size()}
&lt;/c:forEach&gt;
</code></pre>

<p>So the first time I execute the page, <code>personController.groupedPersons</code> returns the right list of persons (a <code>HashMap&lt;Integer, List&lt;Person&gt;&gt;</code>) and prints it just fine. At this point I have 2 groups: a group of 3 Person of the same age and another Person with a different age. When I click on <em>remove</em> the group of the 3 persons with the same age, I trace the code and with the Iterator it removes all the necessary persons without raising a <code>ConcurrentModificationException</code>. The returned list of <code>person.getFriends();</code> is size = 1, which is correct. Then the ajax code renders the form. <code>personController.groupedPersons</code> is called again and returns 1 person as expected. I have verified and this is the return person I actually expect. But then the JSF does print the wrong <code>#{personG.key}</code> (the one I removed) and null <code>#{personG.value.size()}</code>.</p>

<p>I know it might be difficult to follow, but can you think of any explanations for this?</p>

<hr>

<p><strong>EDIT 3:</strong>
This is even funnier... If I delete the group that has 1 person, it is removed and then the JSF prints correctly the group with 3 persons. If I delete the group with 3 persons, they are removed and JSF prints (as I said in EDIT2) the <code>key</code> of the group I just removed and size() is null. Is it possible that I am on a concurrency problem here between JSF refreshing the page at the same time as I am modifying the list?  (this was originally my worry, that the <code>ConcurrentModificationException</code> came from a concurrency problem between the XHTML and my managed bean, and not only within the code of the Managed Bean). That could explain why I got a <code>ConcurrentModificationException</code> even when I added in the list instead of removing from it.</p>