Exercise: A mutability problem
What operation on name
will make the following code print false
?
fun main() {
val set = mutableSetOf<Name>()
val name = Name("AAA")
set.add(name)
// ???
println(set.contains(name)) // should print false
println(set.first() == name) // should print true
}
data class Name(var name: String)
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in effective/safe/NameAndSet.kt. You can find there starting code.
Once you are done with the exercise, you can check your solution here.
fun main() {
val set = mutableSetOf<Name>()
val name = Name("AAA")
set.add(name)
// ???
println(set.contains(name)) // should print false
println(set.first() == name) // should print true
}
data class Name(var name: String)
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.