Map.mapTo to another map

I want to map Map<DAO, Int> to Map<String, Boolean> , but I cannot return Map.Entry to the map function:

 itemsWithQuantity.mapTo(mutableMapOf<String, Boolean>(), { it.key.toString() to it.value != 0 }) 

(of course, I use a more complex display function, but that doesn't matter, the problem is the same)

It says

 MutableMap<String, Boolean> is not a subtype of MutableCollection<Pair<String, Boolean>>. 

So how can I return Map.Entry instead of a pair?

Now I do this:

 val detailsIds = mutableMapOf<String, Boolean>() itemsWithQuantity.forEach { item, quantity -> detailsIds.put(it.key.toString(), it.value != 0) } 

But I want to use mapTo

+5
source share
1 answer

Use associateTo instead:

 xs.associateTo(mutableMapOf<String, Boolean>(), { "${it.key}" to (it.value != 0) }) 

Also note the brackets around it.value != 0 .

The mapTo function, like map , does not collect the results in map , but instead works with Collection , expecting you to provide a MutableCollection<Pair<String, Boolean>> .

+10
source

Source: https://habr.com/ru/post/1265393/


All Articles