FindBugs - how to solve EQ_COMPARETO_USE_OBJECT_EQUALS

前端 未结 5 1765
后悔当初
后悔当初 2021-01-11 14:50

I am clueless here...

 1: private static class ForeignKeyConstraint implements Comparable {
 2: String tableName;
 3: String fkFi         


        
5条回答
  •  旧时难觅i
    2021-01-11 15:08

    You can solve it by implementing an equals() method. Refer to the FindBugs definition:

    "Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue."

    "It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))."

    Another example is the TreeSet. It implements equality checks by invoking compareTo, and a compareTo implementation that is inconsistent with equals makes the TreeSet violate the contract of the Set interface, which might lead to program malfunction.

提交回复
热议问题