title: How to fix ConcurrentModificationException error when adding items during iteration
body: <p>So I am getting a "java.util.ConcurrentModificationException at java.util.ArrayList$Itr.next(ArrayList.java:860)" error. </p>
  
  <p>I know the reason I am getting this error is because I am iterating over a list at the same time that I am adding objects to that list. However, I do not know how to add a listIterator to fix this problem.</p>
  
  <p>If someone could show me how to add the iterator, that would be amazing. Thank you!</p>
  
  <p>I have indicated where the error starts in my code</p>
  
  <pre><code>//method to evaluate who you have chatted with and display on screen
  private void readChats() {
      users = new ArrayList&lt;&gt;();
  
      reference = FirebaseDatabase.getInstance().getReference("USERS");
      reference.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
              users.clear();
  
  
              //gets every user snapshot from "USERS" children in firebase and make User obj
              for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                  User user = snapshot.getValue(User.class);
  
                  //for every ID that was stored in textTransacRec list
                  for (String ID : textTransacRec) {
                      //out of all existing users in firebase if a user's ID matches any of the ID's in textTransacRec
                      if(user.getID().equals(ID)) {
                          //if there are any User objects inside users list
                          if(users.size() != 0) {
                              //check to see if the user already exists in list or not by matching w/ existing user ID's
                              //if there is no ID match then user does not exist --&gt; add to users list to display
                              //---------------------------ERROR STARTS FROM HERE----------------------
                              for(User u : users) {
                                  if(!user.getID().equals(u.getID())) {
                                      users.add(user);
                                  }
                              }
                          }
                          //if no User objects inside users list --&gt; add to users list
                          else {
                              users.add(user);
                          }
                      }
                  }
              }
              chatRecyclerAdap = new ChatRecyclerAdap(getContext(), users);
              recyclerView.setAdapter(chatRecyclerAdap);
          }
  
          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {
  
          }
      });
  }
  </code></pre>