Kotlin 复制列表,但修改字段
我有一张Kotlin 复制列表,但修改字段,kotlin,transformation,Kotlin,Transformation,我有一张人的名单 我想将每个人的状态从“活动”更改为“非活动”。我无法修改原始数据结构或原始数据 fun changeClone(list: List<People>) { val newList = MutableList<People>() list.forEach { person -> //i feel there has to be an easier faster way to do this in kotlin
人的名单
我想将每个人的状态从“活动”
更改为“非活动”
。我无法修改原始数据结构或原始数据
fun changeClone(list: List<People>) {
val newList = MutableList<People>()
list.forEach { person ->
//i feel there has to be an easier faster way to do this in kotlin
val newPerson(person.name, ...., status = "not-active")
newList.add(newPerson)
}
showUi(newList)
}
我可以将val状态
转换为var
,但实际上我不应该修改原始数据。那么,我能做些什么使这更容易呢
另外,所有其他数据必须相同。我没有检查,但是
list.map{it.copy(status=“not active”)}
应该可以工作。见文件:(“复制”部分)。
val newList = list.map { person ->
person.copy(status = "not-active")
}
val newList = list.map { person ->
person.copy(status = "not-active")
}