如何在Kotlin中将消费者放入地图中?

huangapple go评论61阅读模式
英文:

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&lt;T, Consumer&lt;List&lt;T&gt;&gt;&gt;()

 map.put(type, (list) -&gt; {
      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&lt;T, Consumer&lt;List&lt;T&gt;&gt;&gt;()

 map.put(type, (list) -&gt; {
      repository.save(doMappingToOtherEntity(list))
    })

答案1

得分: 1

这是您要翻译的内容:

"Did you tried to use just this:

map[type] = Consumer { list ->
repository.save(doMappingToOtherEntity(it))
}

Full example."

英文:

Did you tried to use just this:

map[type] = Consumer { list -&gt;
    repository.save(doMappingToOtherEntity(it))
}

Full example.

huangapple
  • 本文由 发表于 2023年6月5日 19:36:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定