Jim
Jim

Reputation: 4415

How can I find out what keys were missing when a map was processed in Kotlin?

I have a map of items to process:

fun processAll(items: Map<Int, CustomObject>) {
        items.forEach {
            process(anotherSet[it.key])
        }
    }

What I want to do is for keys that were not processed i.e. the anotherSet has keys that were not part of the items I would like to call another method to do something with those.
How can I do this in a Kotlin specific way?

Upvotes: 0

Views: 1885

Answers (1)

Ilya
Ilya

Reputation: 23144

I suppose anotherSet is actually a Map, given how you use it. Then you can get the residual map from it by subtracting all the keys that are contained in the items map:

val remaining = anotherSet - items.keys
for ((key, value) in remaining) {
   ...
}

You can play with the runnable example here: https://pl.kotl.in/DQVanLYrp

Upvotes: 3

Related Questions