Vaibhavrtk Vaibhavrtk - 1 year ago 70
Scala Question

Which is faster for comparing case class object in scala : a) equals(==) method or b) equating hash values

Which is faster for comparing object in scala :

a) comparing two case class objects using the equals(==) method or

b) creating hash out of the member variables of object and then comparing that hash.

I think equals method should be faster but my friend argues if we create a string of member variables values and generate its hash and compare it it will be faster as it will do one comparison instead of n comparison which it does in equal method. n being the number of variables.

dit dit
Answer Source

These two strings have the same hashcode:

hashcode: 2173622

So you have to use equals to compare to object. In case of the string, if hashcode is cashed, you can first compare them. In this case, if hashcodes are not equal, you can be 100% sure the string are not equal to.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download