// Step 1: Import the HashMap class
import java.util.HashMap
fun main() {
// Step 2: Create a HashMap instance with keys of type String and values of type Int
val myHashMap: HashMap<String, Int> = HashMap()
// Step 3: Add key-value pairs to the HashMap
myHashMap["One"] = 1
myHashMap["Two"] = 2
myHashMap["Three"] = 3
// Step 4: Access and print values using keys
println("Value associated with key 'Two': ${myHashMap["Two"]}")
// Step 5: Check if a key is present in the HashMap
val keyToCheck = "Four"
if (myHashMap.containsKey(keyToCheck)) {
println("$keyToCheck is present with value ${myHashMap[keyToCheck]}")
} else {
println("$keyToCheck is not present in the HashMap")
}
// Step 6: Remove a key-value pair from the HashMap
val keyToRemove = "Two"
myHashMap.remove(keyToRemove)
println("After removing key '$keyToRemove', HashMap: $myHashMap")
// Step 7: Iterate through the HashMap using a loop
println("Iterating through the HashMap:")
for ((key, value) in myHashMap) {
println("Key: $key, Value: $value")
}
}