[英]Merge two different data class lists in to the the third one Kotlin
I want to add additional field to my new data class from the old one which has an extra field of balance value.我想从具有额外余额值字段的旧数据类中向我的新数据类添加额外字段。 I have created data class list with the ID and the Values (balance), which I want to merge first data class list with the second to make 3rd list - similarly like union in sql.
我已经创建了带有 ID 和值(余额)的数据类列表,我想将第一个数据类列表与第二个数据类列表合并以制作第三个列表 - 类似于 sql 中的联合。
first data class第一个数据类
data class Coin(
val id: String,
val price_usd: Double,
val name: String,
val rank: Int,
val symbol: String
)
second data class第二类数据
data class CurrencyBalance(
val id: String,
val quantity: String
)
The aim is to create this type of data class list目的是创建这种类型的数据类列表
data class CoinData(
val id: String,
val price_usd: Double,
val name: String,
val rank: Int,
val symbol: String,
val quantity: String
)
Is something like this what you are looking for?你正在寻找这样的东西吗?
fun convert(currencyBalances: List<CurrencyBalance>, coins: List<Coin>): List<CoinData> =
currencyBalances.map { currencyBalance ->
coins.firstOrNull { coin -> coin.id == currencyBalance.id }
?.let { coin -> CoinData(coin.id, coin.price_usd, coin.name, coin.rank, coin.symbol, currencyBalance.value) }
}.filterNotNull()
I assumed here that the fields in a CurrencyBalance
are called id
and value
我在这里假设
CurrencyBalance
中的字段称为id
和value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.