How do I map a List into a Map? #165
-
Hi I want to transform this into a Map<Application, ObjectB> How can I achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @berickus, thanks for the question. Unfortunatly, mappie has no support for mapping Lists to Maps yet. I will think about an approach to simplify mapping collections to other collections, similar to list to list, and set to set. If you might have any suggestions, they would be appreciated. Currently, I would write what you need as follows: data class ObjectA(val application: String, val name: String)
data class ObjectB(val name: String)
object Mapper : ObjectMappie<List<ObjectA>, Map<String, ObjectB>>() {
override fun map(from: List<ObjectA>) = from.associateBy(ObjectA::name) { ObjectBMapper.map(it) }
// Or move outside Mapper if it's not only required inside Mapper.
private object ObjectBMapper : ObjectMappie<ObjectA, ObjectB>()
}
I would say that this is too verbose and does not add much value over writing a mapper manually. The advantage of the approach above is that you could reuse |
Beta Was this translation helpful? Give feedback.
Hi @berickus, thanks for the question.
Unfortunatly, mappie has no support for mapping Lists to Maps yet. I will think about an approach to simplify mapping collections to other collections, similar to list to list, and set to set. If you might have any suggestions, they would be appreciated.
Currently, I would write what you need as follows: