TeeKai
TeeKai

Reputation: 691

Overriding the Equals in Scala

I need to have a custom equal method for a few of case classes. After some online searching, I haven't see any definite solution. Shall I write my own method of equality check?

Upvotes: 0

Views: 568

Answers (1)

marcospereira
marcospereira

Reputation: 12214

Yes. You will need to define your own equals method:

case class Person(...) {

    override def equals(other: Any): Boolean = {
        ...
    }

    override def hashCode: Int = {
        ...
    }
}

The equals method looks easy, but it could also be trick. I strongly advise you to read the following chapter of Scala Cookbook about how to Define an equals Method (Object Equality) and this essay at Artima: How to Write an Equality Method in Java.

Upvotes: 2

Related Questions