Skip to content

Tag: comparable

compareTo: treat two nulls as equal

I would like my compare method to work so that in the case where field1 == null in both objects, comparation would not be determined, but field2 would be checked, and so on. How to do it in the simplest way? The method below does not work well: when the fields from both objects are null, the comparison is always

java.lang.Comparable and equals

If I implement java.lang.Comparable for a class, do I still have to override the equals() method? Or will the Comparable work for equals as well? If the answer is no, then what if some discrepancy arises? Let’s say the way I term two objects as equal within the equals() method is different from the way I term two objects of