Skip to main content

How to use a custom object as key or value in a HashMap in Kotlin

How to use a custom object as key or value in a HashMap in Kotlin.

Here's a detailed step-by-step tutorial on how to use a custom object as a key or value in a HashMap in Kotlin.

Step 1: Define a Custom Object

First, you need to define a custom object that you want to use as a key or value in the HashMap. For example, let's say you want to create a HashMap where the keys are instances of a class called "Person" and the values are their corresponding ages. Here's an example of how you can define the "Person" class:

class Person(val name: String, val age: Int) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
val person = other as Person
return name == person.name && age == person.age
}

override fun hashCode(): Int {
return name.hashCode() + age
}
}

In this example, the "Person" class has two properties: "name" of type String and "age" of type Int. We also override the equals() and hashCode() methods to ensure proper comparison and hashing of objects.

Step 2: Create a HashMap

Next, you need to create a HashMap to store your custom objects as keys or values. Here's an example of how you can create a HashMap where the keys are instances of the "Person" class and the values are their corresponding ages:

val personMap = HashMap<Person, Int>()

In this example, we define a HashMap called "personMap" with keys of type "Person" and values of type Int.

Step 3: Add Key-Value Pairs to the HashMap

Once you have created the HashMap, you can add key-value pairs to it. Here's an example of how you can add key-value pairs to the "personMap" HashMap:

val person1 = Person("John", 25)
val person2 = Person("Jane", 30)

personMap[person1] = person1.age
personMap[person2] = person2.age

In this example, we create two instances of the "Person" class, "person1" and "person2". We then add these instances as keys to the "personMap" HashMap, with their corresponding ages as values.

Step 4: Access Values from the HashMap

Once you have added key-value pairs to the HashMap, you can access the values using the keys. Here's an example of how you can access values from the "personMap" HashMap:

val age1 = personMap[person1]
val age2 = personMap[person2]

println("Age of ${person1.name}: $age1")
println("Age of ${person2.name}: $age2")

In this example, we use the "person1" and "person2" instances as keys to retrieve their corresponding ages from the "personMap" HashMap. We then print out the ages of the persons.

Step 5: Modify Values in the HashMap

You can also modify the values associated with the keys in the HashMap. Here's an example of how you can modify a value in the "personMap" HashMap:

personMap[person1] = 26

val updatedAge = personMap[person1]
println("Updated age of ${person1.name}: $updatedAge")

In this example, we update the age of "person1" in the "personMap" HashMap. We then retrieve the updated age and print it out.

Step 6: Remove Key-Value Pairs from the HashMap

If you want to remove a specific key-value pair from the HashMap, you can use the "remove()" method. Here's an example of how you can remove a key-value pair from the "personMap" HashMap:

personMap.remove(person1)

val removedAge = personMap[person1]
println("Removed age of ${person1.name}: $removedAge")

In this example, we remove the key-value pair associated with "person1" from the "personMap" HashMap. We then try to retrieve the age of "person1" again, which will be null since the key-value pair has been removed.

That's it! You now know how to use a custom object as a key or value in a HashMap in Kotlin.