Reputation: 2128
I want to convert the value of MutableList
to HashMap
. Can you help me with this? This is my code:
private fun getDestinationFund(destinationFund: List<DestinationFundEntity>) {
val list = mutableListOf<FundsModel>()
for (i in destinationFund) {
list.add(
FundsModel(
ljiId = i.ljiId,
name = i.productName,
mduPercent = i.percentage
)
)
}
convertListToMap(list)
}
private fun convertListToMap(list: MutableList<FundsModel>) {
val map: HashMap<String?, FundsModel> = HashMap()
// Need to convert "list" from params to "map" from "val"
// So, the Live data get data from "map", not from "list"
_currentFundsList.postValue(ArrayList(map.values))
}
Upvotes: 0
Views: 1532
Reputation: 62924
You can use List.map()
to transform your list into a list of Pair
s, and then use toMap()
on that list:
listOf(1,2,3,4)
.map { it -> it to it * 2 }
.toMap()
// produces: {1=2, 2=4, 3=6, 4=8}
Upvotes: 1
Reputation: 2128
Fixed using this:
private fun convertListToMap(list: MutableList<FundsModel>) {
val map: HashMap<String?, FundsModel> = HashMap()
list.forEach {
map[it.name] = it
}
_currentFundsList.postValue(ArrayList(map.values))
}
Upvotes: 0