Android 将一种项目类型的列表转换为另一种项目类型的列表
安卓工作室3.6 片段:Android 将一种项目类型的列表转换为另一种项目类型的列表,android,kotlin,Android,Kotlin,安卓工作室3.6 片段: private fun createHistoryList(participantsEffectResourceList: List<ParticipantsEffectResource>) { val historyList = mutableListOf<MyHistoryItem>() participantsEffectResourceList.forEach({ val date
private fun createHistoryList(participantsEffectResourceList: List<ParticipantsEffectResource>) {
val historyList = mutableListOf<MyHistoryItem>()
participantsEffectResourceList.forEach({
val date = it.operation.appliedAt
val details = it.operation.details
val myHistoryItem = MyHistoryItem(date, details)
historyList.add(myHistoryItem)
})
}
private fun createHistoryList(participantsEffectResourceList:List){
val historyList=mutableListOf()
参与者有效资源列表.forEach({
val date=it.operation.appliedAt
val details=it.operation.details
val myHistoryItem=myHistoryItem(日期、详细信息)
historyList.add(myHistoryItem)
})
}
如您所见,我从一个项目类型(ParticipantsEffectResource
)中提取属性,并将其转换为另一个项目列表(MyHistoryItem
)
很好。很好
但还有其他更简单的方法吗?例如,通过
participantsEffectResourceList.map{}
或smt-else?List
已经提供了.map()
您可以使用:
fun createHistoryList(participantsEffectResourceList: List < ParticipantsEffectResource > ) {
val historyList = participantsEffectResourceList.map {
MyHistoryItem(it.operation.appliedAt, it.operation.details)
}
}
fun createHistoryList(ParticipantSeEffectResourceList:List){
val historyList=participantsEffectResourceList.map{
MyHistoryItem(it.operation.appliedAt,it.operation.details)
}
}
列表已经提供了.map{}