Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the standard idiom for implementing equals and hashCode in Scala?

What is the standard idiom for implementing the equals and hashCode methods in Scala?

I know the preferred approach is discussed in Programming in Scala, but I don't currently have access to the book.

like image 506
Jim Hurne Avatar asked Sep 10 '11 09:09

Jim Hurne


People also ask

What is the hashCode () and equals () used for?

The equals() and hashcode() are the two important methods provided by the Object class for comparing objects. Since the Object class is the parent class for all Java objects, hence all objects inherit the default implementation of these two methods.

What is hashCode in Scala?

Scala String hashCode() method with example The hashCode() method is utilized to find the hash code of the stated string. Method Definition: int hashCode() Return Type: It returns an integer hash code of the string given.

Which is called first hashCode or equals?

Equals is always called after the hashCode method in a java hashed collection while adding and removing elements. The reason being, if there is an element already at the specified bucket, then JVM checks whether it is the same element which it is trying to put.

What is the difference between hashCode and equals?

The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal. equals() checks if the two object references are same. If two objects are equal then their hashCode must be the same, but the reverse is not true.


1 Answers

There's a free 1st edition of PinS that discuss this subject just as well. However, I think the best source is this article by Odersky discussing equality in Java. The discussion in PinS is, iirc, an abbreviated version of this article.

like image 77
Daniel C. Sobral Avatar answered Oct 12 '22 02:10

Daniel C. Sobral