Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kotlin how convert map with long and Pair to other map?

I have a construct like this:

     var values = mutableMapOf<Long, Pair<String, Boolean>>()

No I need to have one map from this above like this:

     val transformedMap = Map<Long, String>

I cannot transform this first to second. Andy suggestions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Most of iterators will require a variable for storing the resulting map, but I think associate can do it directly:

values.entries.associate {
    it.key to it.value.first
}

The other options are like this

val output = mutableMapOf<Long, String>()

//values iterator, for, forEach, keys, etc
output[key] = values[key]?.first
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading