What is hashCode method in Scala?
We can use the hashCode() method is used to calculate the
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.
hashCodeof 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 hashcodeval result = "Hello".hashCode()println(result)// integer hashcodeval result1 = 123.hashCode()println(result1)}
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved