Hey, Kotlin Lovers🖖🏽
In this article, I will share with you some brief about Null Safety, Extension Functions, Lists, and HashMaps in Kotlin language.
Let’s start with my favorite is Null Safety;
Null Safety
Sometimes we create variables or get variables from models. They may not have initials. If we do not check our variables, our application will crash. For more information, you can check Kotlin's official website.
Extension Functions
When you want to create new fun from an existing class is it possible with an extension?
When the string contains a specific char like that “-” and you want to separate the string from the char. You could use something like the above function. You can call from everywhere, for more
Create a List
If you want to add any element to your list. You have to create a list from mutableList()
If you want to create a list with a size limit and want all elements to be the same value.
Convert to string with to the char list using the toCharArray()
distinct() method is getting the unique elements.
One more example :)
Create a HashMap
When you need an element with the key you can use HashMap. Your key type and your value type are manageable by you.
Here’s how a HashMap works:
- Keys: Each value stored in a HashMap is associated with a unique key. Keys must be special within the HashMap, meaning that you cannot have two key-value pairs with the same key.
- Values: Values are the data associated with each key. They can be of any data type, and multiple keys can have the same value.
- Hashing: Behind the scenes, a hash function is used to convert each key into an index in an array. This process is called “hashing.” The goal of hashing is to efficiently locate the value associated with a given key.
- Storage: HashMaps use an underlying array to store key-value pairs. The hash function determines the index where each key-value pair should be stored. If two keys produce the same hash code (a collision), they are stored in the same index using a data structure like a linked list or a balanced tree.
- Retrieval: To retrieve a value from a HashMap, you provide the key associated with that value. The HashMap uses the same hash function to determine where the value is stored and returns it.
We came to the end of the mine note.
Have an excellent reading 🎉