listkotlinconsumermutablemap

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(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))
    })

Solution

  • Did you tried to use just this:

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

    Full example.