Kotlin HashMap is a collection which contains pairs of object. Kotlin Hash Table based implementation of the MutableMap interface. It stores the data in the form of key and value pair. Map keys are unique and the map holds only one value for each key. It is represented as HashMap<key, value> or HashMap<K, V>.
The hash table based implementation of HashMap does not guarantees about the order of specified data of key, value and entries of collections.
Constructors of Kotlin HashMap class –
Kotlin HashMap provides 4 constructors and access modifier of each is public:
Use of HashMap Functions –
Kotlin program of using functions HashMap(), HashMap(original: Map), Traversing hashmap, HashMap.get() –
fun main(args: Array<String>) { //A simple example of HashMap class define // with empty "HashMap of <String, Int>" var hashMap : HashMap<String, Int> = HashMap<String, Int> () //printing the Empty hashMap printHashMap(hashMap) //adding elements to the hashMap using // put() function hashMap.put("IronMan" , 3000) hashMap.put("Thor" , 100) hashMap.put("SpiderMan" , 1100) hashMap.put("NickFury" , 1200) hashMap.put("HawkEye" , 1300) //printing the non-Empty hashMap printHashMap(hashMap) //using the overloaded print function of //Kotlin language to get the same results println("hashMap : " + hashMap + "\n") //hashMap traversal using a for loop for(key in hashMap.keys){ println("Element at key $key : ${hashMap[key]}") } //creating another hashMap object with // the previous version of hashMap object var secondHashMap : HashMap<String, Int> = HashMap<String, Int> (hashMap) println("\n" + "Second HashMap : ") for(key in secondHashMap.keys){ //using hashMap.get() function to fetch the values println("Element at key $key : ${hashMap.get(key)}") } //this will clear the whole map and make it empty println("hashMap.clear()") hashMap.clear() println("After Clearing : " + hashMap) } //function to print the hashMap fun printHashMap(hashMap : HashMap<String, Int>){ // isEmpty() function to check whether // the hashMap is empty or not if(hashMap.isEmpty()){ println("hashMap is empty") }else{ println("hashMap : " + hashMap) } } |
Output :
hashMap is empty : {}
hashMap : {Thor=100, HawkEye=1300, NickFury=1200, IronMan=3000, SpiderMan=1100}
hashMap : {Thor=100, HawkEye=1300, NickFury=1200, IronMan=3000, SpiderMan=1100}
Element at key Thor : 100
Element at key HawkEye : 1300
Element at key NickFury : 1200
Element at key IronMan : 3000
Element at key SpiderMan : 1100
secondHashMap :
Element at key Thor : 100
Element at key HawkEye : 1300
Element at key IronMan : 3000
Element at key NickFury : 1200
Element at key SpiderMan : 1100
hashMap.clear()
After Clearing : {}
Kotlin program of using HashMap initial capacity, HashMap.size –
fun main(args: Array<String>) { //HashMap can also be initialize // with its initial capacity. //The capacity can be changed by // adding and replacing its element. var hashMap : HashMap<String, Int> = HashMap<String, Int> (4) //adding elements to the hashMap using put() function hashMap.put("IronMan" , 3000) hashMap.put("Thor" , 100) hashMap.put("SpiderMan" , 1100) hashMap.put("NickFury" , 1200) for(key in hashMap.keys) { println("Element at key $key : ${hashMap[key]}") } //returns the size of hashMap println("\n" + "hashMap.size : " + hashMap.size ) //adding a new element in the hashMap hashMap["BlackWidow"] = 1000; println("hashMap.size : " + hashMap.size + "\n") for(key in hashMap.keys) { println("Element at key $key : ${hashMap[key]}") } } |
Output:
Element at key Thor : 100 Element at key IronMan : 3000 Element at key NickFury : 1200 Element at key SpiderMan : 1100 hashMap.size : 4 hashMap.size : 5 Element at key Thor : 100 Element at key BlackWidow : 1000 Element at key IronMan : 3000 Element at key NickFury : 1200 Element at key SpiderMan : 1100
Kotlin program of using functions HashMap.get(key), HashMap.replace(), HashMap.put() –
fun main(args: Array<String>) { var hashMap : HashMap<String, Int> = HashMap<String, Int> () //adding elements to the hashMap // using put() function hashMap.put("IronMan" , 3000) hashMap.put("Thor" , 100) hashMap.put("SpiderMan" , 1100) hashMap.put("Cap" , 1200) for(key in hashMap.keys) { println("Element at key $key : ${hashMap[key]}") } //the hashMap's elements can be accessed like this println("\nhashMap[\"IronMan\"] : " + hashMap["IronMan"]) hashMap["Thor"] = 2000 println("hashMap.get(\"Thor\") : " + hashMap.get("Thor") + "\n") //replacing some values hashMap.replace("Cap" , 999); hashMap.put("Thor" , 2000); println("hashMap.replace(\"Cap\" , 999)" + " hashMap.replace(\"Thor\" , 2000)) :") for(key in hashMap.keys) { println("Element at key $key : ${hashMap[key]}") } } |
Output:
Element at key Thor : 100
Element at key Cap : 1200
Element at key IronMan : 3000
Element at key SpiderMan : 1100
hashMap["IronMan"] : 3000
hashMap.get("Thor") : 2000
hashMap.replace("Cap", 999) hashMap.replace("Thor", 2000)) :
Element at key Thor : 2000
Element at key Cap : 999
Element at key IronMan : 3000
Element at key SpiderMan : 1100
Time complexity of HashMap –
Kotlin HashMap provides constant time or O(1) complexity for basic operations like get and put, if hash function is properly written and it disperses the elements properly. In case of searching in the HashMap containsKey() is just a get() that throws away the retrieved value, it’s O(1) (assuming the hash function works properly).
Some other functions of Kotlin HashMap class –
Recommended Posts:
- Initialize HashMap in Java
- Traverse through a HashMap in Java
- How to create a Java HashMap of user defined class type?
- HashMap replace(key, oldValue, newValue) method in Java with Examples
- HashMap replace(key, value) method in Java with Examples
- HashMap putIfAbsent(key, value) method in Java with Examples
- HashMap forEach(BiConsumer) method in Java with Examples
- HashMap merge(key, value, BiFunction) method in Java with Examples
- HashMap getOrDefault(key, defaultValue) method in Java with Examples
- HashMap computeIfPresent(key, BiFunction) method in Java with Examples
- HashMap replaceAll(BiFunction) method in Java with Examples
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- DatePicker in Kotlin
- Kotlin labeled continue
- Introduction to Kotlin
- Kotlin Type Conversion
- Kotlin Exception Handling | try, catch, throw and finally
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

