英文:
How to put a consumer in a map in Kotlin?
问题
I come from java and I'm new in kotlin.
I was trying to create a map and put a consumer inside as in these examples:
https://stackoverflow.com/questions/44422685/consumert-mapped-classt-in-hashmap
https://stackoverflow.com/questions/46464820/how-to-write-java-8-consumer-to-work-with-collectionmap-of-consumer
but using kotlin.
So far I haven't done it successfully.
Here is an example of my code:
val map = mutableMapOf<T, Consumer<List<T>>>()
map.put(type, { list ->
repository.save(doMappingToOtherEntity(list))
})
But "list" is in red and everything in {...} is in grey.
Do you know if what I'm trying to do is possible in Kotlin? If it is, what I'm doing wrong? and if it's not possible, what other alternative would you suggest?
Thanks!
EDIT: I realized the code provided is not correct so here it is:
val map = mutableMapOf<T, Consumer<List<T>>>()
map.put(type, { list ->
repository.save(doMappingToOtherEntity(list))
})
英文:
I come from java and I'm new in kotlin.
I was trying to create a map and put a consumer inside as in these examples:
https://stackoverflow.com/questions/44422685/consumert-mapped-classt-in-hashmap
https://stackoverflow.com/questions/46464820/how-to-write-java-8-consumer-to-work-with-collectionmap-of-consumer
but using kotlin.
So far I haven't done it successfully.
Here is an example of my code:
val map = mutableMapOf<T, Consumer<List<T>>>()
map.put(type, (list) -> {
repository.save(doMappingToOtherEntity(it))
})
But "list" is in red and everything in "{...}" is in grey
Do you know if what I'm trying to do is possible in Kotlin? If it is, what I'm doing wrong? and if it's not possible, what other alternative would you suggest?
Thanks!
EDIT: I realized the code provided is not correct so here it is:
val map = mutableMapOf<T, Consumer<List<T>>>()
map.put(type, (list) -> {
repository.save(doMappingToOtherEntity(list))
})
答案1
得分: 1
这是您要翻译的内容:
"Did you tried to use just this:
map[type] = Consumer { list ->
repository.save(doMappingToOtherEntity(it))
}
英文:
Did you tried to use just this:
map[type] = Consumer { list ->
repository.save(doMappingToOtherEntity(it))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论