Consider the following code:
import scala.collection.mutable
case class A(b: Int) {
override def equals(obj: Any): Boolean = {
println("called")
obj match {
case o: A => b == o.b
case _ => false
}
}
}
val set = mutable.Set.empty[A]
val a1 = A(1)
set.add(a1)
println(set.contains(A(1)))
println(set.contains(A(2)))
Why the second call of set.contains
doesn't print out "called"? Can someone explain how set identify two objects being equal?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…