简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM