title: How to fix concurrent modification exception in Kotlin
body: <p>This is my code, in which I am adding data to a list:</p>
  
  <pre><code>fun bindProcess(listOfDocId: MutableList&lt;String&gt;, isActvityLogEnabled: Boolean): Observable&lt;MutableList&lt;SearchResultModel&gt;&gt; {
      return Observable.create&lt;MutableList&lt;SearchResultModel&gt;&gt; {
  
          var filteredList =CopyOnWriteArrayList&lt;SearchResultModel&gt;()// mutableListOf&lt;SearchResultModel&gt;()
          val headerItem: SearchResultModel = SearchResultModel()
          headerItem.type = 0
          headerItem.title = context.getString(R.string.top_matches_label)
          filteredList.add(headerItem)
  
          for (row in ContSyncApplication.getContacts()) {
              if (listOfDocId.contains(row.docId)) {
                  val myData: SearchResultModel = SearchResultModel()
                  myData.type = 1
                  myData.docId = row.docId
                  myData.name = row.display_name
  
                  if (!row.profile_image.isNullOrBlank()) {
                      myData.imagePath = row.profile_image!!
                  }
                  filteredList.add(myData)
  
                  if (isActvityLogEnabled &amp;&amp; DatabaseQueryHelper.checkActivityLogById(row.docId)) {
                      val myactivityData: SearchResultModel = SearchResultModel()
                      myactivityData.type = 1
                      myactivityData.docId = row.docId
                      myactivityData.name = row.display_name
                      myactivityData.imagePath = row.profile_image ?: ""
                      mActvityList.add(myactivityData)
                  }
              }
          }
  
          if (mActvityList.size &gt; 0) {
              val activityHeader: SearchResultModel = SearchResultModel()
              activityHeader.type = 0
              activityHeader.title = "Activity Log"
              filteredList.add(activityHeader)
              filteredList.addAll(mActvityList)
          }
  
          it.onNext(filteredList)
          it.onComplete()
      }
  }
  </code></pre>
  
  <p>While I execute and add data in the list I am getting a <code>ConcurrentModificationException</code>. Can anyone suggest to me how to execute the loop so that I can fix this issue? I have to search and add to the list.</p>