Kotlin, Software Development

Kotlin – Check if key exists in Map

This post shows sample codes that check if a key exists in a map. There are 3 ways to do this in Kotlin. We are using Kotlin 1.3.

Map.containsKey(key: String)

Map.contains(key: String)

This one is a bit special. The documentation says:

x in map

Output:

References

https://kotlinlang.org/docs/reference/collections.html

You Might Also Like

4 Comments

  1. 1
  2. 2
  3. 3

    Hey FYI, you can embed the Kotlin Playground directly into your page, and allow users to edit/run code snippets. I understand if you don’t want that for consistency elsewhere on your site, just a thought.

  4. 4

Leave a Reply

Your email address will not be published. Required fields are marked *