For loop 在Kotlin的嵌套for循环中满足If条件时,将数据添加到arrayList

For loop 在Kotlin的嵌套for循环中满足If条件时,将数据添加到arrayList,for-loop,kotlin,arraylist,hashmap,nested-loops,For Loop,Kotlin,Arraylist,Hashmap,Nested Loops,检查以下代码: getCommonsArrayList(listA:ArrayList< User >, listB:ArrayList<User>):ArrayList<User>{ var listCommon = ArrayList<User>() for (i in listA.indices) { for (j in listB.indices) { if (listA[i]

检查以下代码:

 getCommonsArrayList(listA:ArrayList< User >, listB:ArrayList<User>):ArrayList<User>{

    var listCommon = ArrayList<User>()

    for (i in listA.indices) { 
        for (j in listB.indices) {
            if (listA[i].id.equals(listB[j].id)) { //if id of the user matches
                listCommon.put(listA[i]) //add to a new list 
            }
        }
    }

    return listCommon // return the new list with common entries
}
getCommonArrayList(listA:ArrayList,listB:ArrayList):ArrayList{
var listcomon=ArrayList()
对于(列表索引中的i){
对于(列表B.索引中的j){
if(listA[i].id.equals(listB[j].id)){//如果用户的id匹配
listcomon.put(listA[i])//添加到新列表
}
}
}
return listCommon//返回带有公共项的新列表
}
上面的方法迭代列表a和b并检查id是否匹配,如果匹配,则将用户对象存储到新列表中,并在程序结束时返回公共列表。 这件事很有效。我希望嵌套,然后是if条件是比较两个列表的方式

问题是如果listA有重复的条目,那么listcomon也会有重复的条目,因为ArrayList支持条目的重复性

为了使commonList唯一,我引入了一个HashMap对象,如下所示:

  getCommonsArrayList(listA:ArrayList< User >, listB:ArrayList<User>):ArrayList<User>{

            var listCommon = ArrayList<User>()

            var arrResponseMap = HashMap<String,User>()


            for (i in listA.indices) { 
                for (j in listB.indices) {
                    if (listA[i].id.equals(listB[j].id)) { //if id of the user matches
                     arrResponseMap.put(listA[i].id,listA[i]) // add id to map so there would be no duplicacy
                    }
                }

             }
      arrResponseMap.forEach {
            listCommon.add(it.value) //iterate the map and add all values 
        }

            return listCommon // return the new list with common entries
   }
getCommonArrayList(listA:ArrayList,listB:ArrayList):ArrayList{
var listcomon=ArrayList()
var arresponsemap=HashMap()
对于(列表索引中的i){
对于(列表B.索引中的j){
if(listA[i].id.equals(listB[j].id)){//如果用户的id匹配
arresponsemap.put(listA[i].id,listA[i])//将id添加到映射中,这样就不会出现重复
}
}
}
阿雷玛·弗雷奇{
listcomon.add(it.value)//迭代映射并添加所有值
}
return listCommon//返回带有公共项的新列表
}
这将提供具有公共Id的userObject的新arrayList。但这比上面的代码复杂得多

如果listAlistB的大小增加到1000,则此执行将花费大量时间


如果有更好的方法可以解决这个问题,请有人指导我。

您可以简单地使用
distinctBy
从列表中仅获取唯一的值

返回仅包含给定序列中元素的序列 具有由给定选择器函数返回的不同键

结果序列中的元素与它们的顺序相同 都在源序列中

以下是一个例子:

        val model1 = UserModel()
        model1.userId = 1
        val model2 = UserModel()
        model1.userId = 2
        val model3 = UserModel()
        model1.userId = 1
        val model4 = UserModel()
        model1.userId = 2

        val commonList = listOf(model1, model2, model3, model4)
        // get unique list based on userID, use any field to base your distinction
        val uniqueList = commonList
            .distinctBy { it.userId }
            .toList()

        assert(uniqueList.count() == 2)
        assert(commonList.count() == 4)

像这样在列表和使用distinctBy中添加

 data class DevelopersDetail(val domain: String, val role: String)
    val d1 = DevelopersDetail("a", "1")
    val d2 = DevelopersDetail("b", "1")
    val d3 = DevelopersDetail("c", "1")
    val d4 = DevelopersDetail("c", "1")
    val d5 = DevelopersDetail("d", "1")

    var listA = listOf(d1, d2, d3, d4)
    var listb = listOf(d1, d2, d3, d4)


    var data = listA + listb
    var list= data
        .distinctBy { it.domain }
        .toList()

    println("list   $list")

   //output-list   [DevelopersDetail(domain=a, role=1), DevelopersDetail(domain=b, role=1), DevelopersDetail(domain=c, role=1)]