Implementing the equals method in java

前端 未结 7 1080
醉酒成梦
醉酒成梦 2021-01-18 15:39

This is my implementation of the equals class for a Coor class which is just contains 2 ints x and y. would this be the proper way of implementing this method?



        
7条回答
  •  误落风尘
    2021-01-18 15:54

    Yes, it would.

    Also be sure to override your hashCode() method--never override one without doing the other as well, it will confuse the hell out of your collections.

    Your case could use a hash where it simply shifts one of the ints 32 bits and adds it to the other creating a completely unique long (a perfect hash function in this case--no collisions)

提交回复
热议问题