I have two lists. One is called oldList
and contains old data, the 2nd one is called updateList
and contains a set of updates.
I want to create a new list with those rules:
oldList
id
id
of an updated item already exists in oldData
, replace itI came up with a simple code:
Item.kt
data class Item (val id: String, val text: String)
Main.kt
fun main() {
val oldList: List<Item> = listOf(
Item("aaa1", "aaa2"),
Item("bbb1", "bbb2"),
Item("ddd1", "ddd2"))
val updateList: List<Item> = listOf(
Item("aaa1", "aaa3"),
Item("ccc1", "ccc2"))
val resultList = oldList.toMutableList()
for (item in updateList) {
val index = oldList.indexOfFirst { it.id == item.id }
if (index < 0) {
resultList.add(item)
} else {
resultList[index] = item
}
}
println(resultList)
}
This works fine but I can imagine it's inefficient and there might also be some nice Kotlin
idiom. Is there a better solution?
One way to merge multiple lists is by using addAll() method of java. util. Collection class, which allows you to add the content of one List into another List. By using the addAll() method you can add contents from as many List as you want, it's the best way to combine multiple List.
In python, we can use the + operator to merge the contents of two lists into a new list. For example, We can use + operator to merge two lists i.e. It returned a new concatenated lists, which contains the contents of both list_1 and list_2.
To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.
If the original order doesn't matter, you can combine the lists, putting the new values first to favor them in a distinctBy
call:
val resultList = (updateList + oldList).distinctBy(Item::id)
If the order matters exactly as you described it, you can convert both lists to maps before combining them. When combining two Maps with +
, the items in the second Map take precedence, but the order from the first Map is preserved.
val resultList =
(oldList.associateBy(Item::id) + updateList.associateBy(Item::id)).values.toList()
You can use the distinctBy
method:
val resultList = (updateList + oldList).distinctBy { it.id }
What distinctBy
does is return a new list containing only elements having distinct keys returned by the provided lambda. You must bear in mind however that the order of elements matters and the first encountered item with the given key is kept in the result list, the others are ignored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With