Left Index

Kotlin HashMap

In Kotlin, A map is a collection that holds pairs of objects. Each pair consists of a key and a value. Map keys are unique, the map holds only one value for each key. Kotlin Hash Table based implementation of the MutableMap interface. It stores the data in the form of key and value pair. If you want to create a new HashMap that allows null values and null keys, just use the question mark ? right after the types.

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap

fun main(args: Array<String>) {
	// create hashmap default object
	var hashMap = HashMap<Int, String>()
	hashMap.put(1, "John")
	hashMap.put(2, "Tony")
	hashMap.put(3, "Sam")
	hashMap.put(4, "Jony")
	
	hashMap.forEach { item -> println("Key: ${item.key}, Value: ${item.value}") }
	println()
	
	// create hashmap object using initial capacity i.e. 10
	var hashMap1 = HashMap<Int, String>(10)
	hashMap1.put(1, "John")
	hashMap1.put(2, "Tony")
	hashMap1.put(3, "Sam")
	hashMap1.put(4, "Jony")

	hashMap1.forEach { item -> println("Key: ${item.key}, Value: ${item.value}") }
	println()
	
	// create hashmap with another hashmap object
	var hashMap2 = HashMap<Int, String>(hashMap1)
	hashMap2.put(5, "Tom")
	hashMap2.forEach { item -> println("Key: ${item.key}, Value: ${item.value}") }
	println()

	// create hashmap with another mapOf object
	var hashMap3 = HashMap<Int, String>(mapOf(1 to "Harry", 2 to "Vampire"))
	hashMap3.put(3, "Tom")
	hashMap3.forEach { item -> println("Key: ${item.key}, Value: ${item.value}") }
}
Output:
Key: 1, Value: John
Key: 2, Value: Tony
Key: 3, Value: Sam
Key: 4, Value: Jony

Key: 1, Value: John
Key: 2, Value: Tony
Key: 3, Value: Sam
Key: 4, Value: Jony

Key: 1, Value: John
Key: 2, Value: Tony
Key: 3, Value: Sam
Key: 4, Value: Jony
Key: 5, Value: Tom

Key: 1, Value: Harry
Key: 2, Value: Vampire
Key: 3, Value: Tom

Add, Get, Modify Example:
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap

fun main(args: Array<String>) {
	// create hashmap default object
	var hashMap = HashMap<Int, String>()
	hashMap.put(1, "John")
	hashMap.put(2, "Tony")
	hashMap.put(3, "Sam")
	hashMap.put(4, "Jony")
	
	// how to replace HashMap key for 2
	hashMap.replace(2, "All is Good")
		
	hashMap.forEach { item -> println("Key: ${item.key}, Value: ${item.value}") }
	println()
	
	// getting value	
	println("Value at 2: ${hashMap.get(2)}")
	println("Value at 5: ${hashMap.getOrElse(5) {"Not found for 5"}}")
	println("Value at 1: ${hashMap.getOrElse(1) {"Not found for 1"}}")
	println("Value at 2: ${hashMap.getOrDefault(2, "Not found for 2")}")
	println("Value at 6: ${hashMap.getOrPut(6) { "Ding Dong" }}")

	// remove item from HashMap
	println("Remove at 2: ${hashMap.remove(2, "Tony")}")
	println("Remove at 2: ${hashMap.remove(2)}")
}
Output:
Key: 1, Value: John
Key: 2, Value: All is Good
Key: 3, Value: Sam
Key: 4, Value: Jony

Value at 2: All is Good
Value at 5: Not found for 5
Value at 1: John
Value at 2: All is Good
Value at 2: Ding Dong
Remove at 2: false
Remove at 2: All is Good