Skip to content

Instantly share code, notes, and snippets.

@patbeagan1
Last active April 30, 2021 21:12
Show Gist options
  • Save patbeagan1/77220cb1e99e1c03d65dc48e1bbd5343 to your computer and use it in GitHub Desktop.
Save patbeagan1/77220cb1e99e1c03d65dc48e1bbd5343 to your computer and use it in GitHub Desktop.
Hashcode generation
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
val test1 = "test1"
val test2 = "test2"
val test3 = "test3"
val test4 = null
open class A {
protected fun Int.lhash(i: Any?) = 31 * this + i.hashCode()
open fun hashcode(): Int = 31
.lhash(test1)
.lhash(test4)
.lhash(test2)
.lhash(test3)
}
class B: A(){
val a = 5
override fun hashcode():Int {
return super.hashcode().lhash(a)
}
}
fun main() {
println("hashCode4=>${A().hashcode()}")
println("hashCode4=>${B().hashcode()}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment