简体   繁体   English

比较和替换Kotlin中大小不同的两个列表中的项目?

[英]Comparing and replacing items in two lists with different sizes in Kotlin?

I have the following function: 我有以下功能:

override fun insertUpdatedItems(items: List<AutomobileEntity>) {
        if (!items.isEmpty()) {
            items.forEachIndexed { index, automobileEntity ->
                if (automobileEntity.id == items[index].id) {
                    automobileCollection[index] = items[index]
                    notifyItemInserted(index)
                }
            }
        }
    }

I'm using to provide data for a recyclerview, I'm trying to insert updated/edited items that are already in automobileCollection which size always returns 10 items but the items list might differ it can be 1 to 10 . 我使用了recyclerview提供的数据,我想插入更新/编辑的项目,已经在automobileCollection其大小总是返回10项,但该items清单可能会有所不同也可以是110

It's supposed to compare items by id but what I'm getting currently with this function is the edited items are just inserted to recyclerview's adapter and not treated as an already existing item. 它应该按id比较项目,但是我目前使用此功能得到的是已编辑的项目只是插入到recyclerview的适配器中,而不被视为已经存在的项目。

On the contrary, if I iterate using automobileCollection I get IndexOutOfBoundsException since most of the time the items list is smaller than automobileCollection . 相反,如果我重复使用automobileCollection我得到IndexOutOfBoundsException异常,因为大部分时间items清单小于automobileCollection

To update a list with items from another one, you can use several ways. 要使用另一个列表中的项目更新列表,可以使用几种方法。

First starting with a direct replacement (which preserves the order, but that's just a detail): 首先从直接替换开始(保留订单,但这只是一个细节):

val sourceList = TODO()
val targetList = TODO()

targetList.replaceAll { targetItem -> 
  sourceList.firstOrNull { targetItem.id == it.id } 
            ?: targetItem
}

Alternatively removing all the items and adding them again: 或者,删除所有项目并再次添加它们:

targetList.removeIf { targetItem ->
  sourceList.any { it.id == targetItem.id }
}
targetList.addAll(sourceList)

Using listIterator (note! that's actually also happening under the hood when you call replaceAll ... not in the same way, but similar ;-)): 使用listIterator(注意!当您调用replaceAll时,实际上也实际上是在replaceAll ……不是以相同的方式,而是类似的;-)):

val iterator = targetList.listIterator()
while (iterator.hasNext()) {
  iterator.next().apply {
    sourceList.firstOrNull { id == it.id }?.also(iterator::set)
  }
}

Probably not so readable... For your forEachIndexed I do not really see any use-case. 可能不是那么可读...对于您的forEachIndexed我真的看不到任何用例。 For other problems there definitely are, but I would suggest you try to omit indices (and also forEach ) as often as you can. 对于其他问题肯定有,但是我建议您尝试尽可能地省略索引(以及forEach )。 If nothing better comes to your mind, then forEach is also ok, but many times, forEach (and even more so forEachIndexed ) isn't the best approach to solve an issue. 如果您没有更好的想法,那么forEach也可以,但是很多时候, forEach (甚至更多的是forEachIndexed )不是解决问题的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM