Ask Your Question
1

What is the typical method in Kotlin for creating a new map with extra key-value pairs added to an existing immutable map?

asked 2022-05-04 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-01-05 00:00:00 +0000

lakamha gravatar image

In Kotlin, the typical method for creating a new map with extra key-value pairs added to an existing immutable map is to use the plus operator.

For example, suppose we have an immutable mapOf a few key-value pairs:

val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)

We can create a new map with an extra key-value pair added to it by using the plus operator:

val map2 = map1.plus("d" to 4)

This creates a new immutable map map2 which includes all the key-value pairs from map1 as well as a new key-value pair "d" to 4. Note that map1 is not modified by this operation as it is immutable.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-04 11:00:00 +0000

Seen: 7 times

Last updated: Jan 05 '23