0

I am getting "comparison method violates its general contract" Exception in my application so I tried to create the dummy compare To method in standalone application as mentioned below but not able to replicate the issue. Can someone please help me to troubleshoot issue, Here I have created Employee Class and implemented java.lang.Comparable interface for that class

public int compareTo(Employee arg0) {

    StringBuffer sb = new StringBuffer();
    StringBuffer sb2 = new StringBuffer();

    sb.append(getEmpId())
        .append(",")
        .append(getFirstName())
        .append(",")
        .append(getLastName());

    sb2.append(arg0.getEmpId())
        .append(",")
        .append(arg0.getFirstName())
        .append(",")
        .append(arg0.getLastName());

    return sb.toString().compareToIgnoreCase(sb2.toString());
}

for this compareTo method I am facing mentioned exception in my enterprise application. Volunteer help is appricated.

I am using JDK version 1.7.0_201.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
Sagar
  • 11
  • So... you found a problem creating a [mcve]...? – Jai Nov 13 '18 at 08:30
  • 1
    This method is very inefficient but seems to be functionally OK. I don't see any contract violations. – Henry Nov 13 '18 at 08:30
  • Have you overridden `Object.equals`? You have to override it such that it returns `true` for each instance where your `compareTo` method returns `0`. The two must be consistent – ernest_k Nov 13 '18 at 08:30
  • 2
    @ernest_k Not entirely. While it is strongly recommended, it's not required. *["It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))"](https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T-)* – MC Emperor Nov 13 '18 at 08:32
  • 2
    The weirdest thing I could imagine is that one of the `Employee` instance is changed when something (e.g. a `TreeMap`) is doing all the comparisons. Basically, this means concurrency issue. Does your original implementation uses multiple threads when handling this? – Jai Nov 13 '18 at 09:02
  • Have you overridden Object.equals? You have to override it such that it returns true for each instance where your compareTo method returns 0. The two must be consistent – ernest_k Can you please provide any references for this – Sagar Nov 13 '18 at 09:17
  • The weirdest thing I could imagine is that one of the Employee instance is changed when something (e.g. a TreeMap) is doing all the comparisons. Basically, this means concurrency issue. Does your original implementation uses multiple threads when handling this? No we are not using threading here – Sagar Nov 13 '18 at 09:27
  • This method is very inefficient but seems to be functionally OK. I don't see any contract violations.--Sometimes we are not getting exception but sometimes we are getting ,so trying to replicate the issue in standalone but not able to do that. – Sagar Nov 13 '18 at 09:30
  • You need to handle Null checks. Also refer these answers here. "Comparison method violates its general contract!" JAVA is smart enough to detect that Nulls are not checked. Hence sometimes this issue occurs – Sumit Vairagar Nov 13 '18 at 10:24

0 Answers0