How default .equals and .hashCode will work for my classes?

前端 未结 6 1715
不思量自难忘°
不思量自难忘° 2020-11-28 23:54

Say I have my own class

public class MyObj { /* ... */ }

It has some attributes and methods. It DOES NOT implement equals, DOES NOT impleme

6条回答
  •  被撕碎了的回忆
    2020-11-29 00:50

    If you do not provide your own implementation, one derived from Object would be used. It is OK, unless you plan to put your class instances into i.e. HashSet (any collection that actually use hashCode() ), or something that need to check object's equality (i.e. HashSet's contains() method). Otherwise it will work incorrectly, if that's what you are asking for.

    It is quite easy to provide your own implementation of these methods thanks to HashCodeBuilder and EqualsBuilder from Apache Commons Lang.

提交回复
热议问题