What is hashCode method in Scala?

We can use the hashCode() method is used to calculate the hash valuea uniquely identified numerical value. of the stated string or a given integer.

Method definition

def hashCode(): Int

Syntax

  • Integer value:
int.hashCode()
  • String value:
String.hashCode()

Calling this method returns an integer value which is the hashCode of the specified string/integer.

hashCode of an integer may be equal to the integer itself.

Code

Let’s have a look at a coding example to understand it further:

object Main extends App {
// string hashcode
val result = "Hello".hashCode()
println(result)
// integer hashcode
val result1 = 123.hashCode()
println(result1)
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved