

You also know way to iterate over a HashMap, filter or transform a HashMap. Now you can create, initialize a HashMap, add, update and remove items from a HashMap. In this Kotlin tutorial, we’ve learned some important points of HashMap. The example show you way to initialize a HashMap using hashMapOf() function: var bzkMap: HashMap = hashMapOf("zero" to 0, "onek" to 1000, "twok" to 2000, "bezkoder" to 9999)

Now it will throw Null pointer exception if we try to use it. If you want to create a new HashMap that allows null values and null keys, just use the question mark ? right after the types. The operator allows us to change a Nullable reference to a Non-null reference. Kotlins variables are all non-nullable by default. To assign a null value to a variable, you must declare a nullable variable type by adding to the end of the base type. In Kotlin, references to objects cannot contain null values by default. We can create a new HashMap using constructor. The goal of Kotlin is to eliminate the possibility of a NullPointerException. Kotlin provides strict nullability rules that maintain type-safety throughout your app. We can add an entry with null value or null key to Kotlin HashMap.The key-value pair coming later could be ordered first. The operation is intermediate and stateless.

Kotlin Hashmap with different value types.
