This is my code, in which I am adding data to a list:
fun bindProcess(listOfDocId: MutableList<String>, isActvityLogEnabled: Boolean): Observable<MutableList<SearchResultModel>> {
    return Observable.create<MutableList<SearchResultModel>> {
        var filteredList =CopyOnWriteArrayList<SearchResultModel>()// mutableListOf<SearchResultModel>()
        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 && 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 > 0) {
            val activityHeader: SearchResultModel = SearchResultModel()
            activityHeader.type = 0
            activityHeader.title = "Activity Log"
            filteredList.add(activityHeader)
            filteredList.addAll(mActvityList)
        }
        it.onNext(filteredList)
        it.onComplete()
    }
}
While I execute and add data in the list I am getting a ConcurrentModificationException. 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.