LinkedHashMap assign to new map with different key

I have a linkedHashMap with the key being an integer and value an object. Instead, I need the final hash map to have a string key. Example:

var test:<Int?, User>
If Int==2 then new map should be : "Admin" -> {/..userobject} for that key.

I have an enum where every int maps to a certain role. But how do I change the key of the LinkedHashMap to be that string role instead of the integer value of it? Can I loop through the linkedhashmap to rename the key?

Answer

Such operation is generally called “mapping” or “transforming” a collection. It applies not only to maps, but to lists and other collections as well.

As you only need to map keys and keep values intact, you can use mapKeys() function in Kotlin:

val input = mapOf(1 to "foo", 2 to "bar")
val result = input.mapKeys { it.key.toString() }

Replace toString() with whatever you use to convert int to a string.