Scala hashCode函数

Scala hashCode函数教程

Scala 中,hashCode 函数用于返回此字符串的哈希码。

字符串hash码

语法

int hashCode()

返回值

返回字符串的 hash 码。

案例

字符串hash码

使用 hashCode 函数,获取字符串的 hash 码

object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") var str1 = "Hello,HaiCoder!" var str2 = "Hello,World!" var hash1 = str1.hashCode() var hash2 = str2.hashCode() printf("hash1 = %d, hash2 = %d\n", hash1, hash2) } }

程序运行后,控制台输出如下:

16_Scala hashCode函数.png

我们使用了 hashCode 函数实现了获取字符串的 hashCode 码。

Scala hashCode函数总结

在 Scala 中,hashCode 函数用于返回此字符串的哈希码。