4

I am trying to write a custom sort function using comparable interface.

My compareTo function is as follows:

@Override
    public int compareTo(MyClass o) {
        return (int)(this.getTimeInMills() - o.getTimeInMills());
    }

I am getting the following exception when I try to sort a list of myClass objects.

28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT java.lang.IllegalArgumentException: Comparison method violates its general contract!
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:866) ~[?:1.8.0_152]
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:483) ~[?:1.8.0_152]
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.ComparableTimSort.mergeForceCollapse(ComparableTimSort.java:422) ~[?:1.8.0_152]
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.ComparableTimSort.sort(ComparableTimSort.java:222) ~[?:1.8.0_152]
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.Arrays.sort(Arrays.java:1312) ~[?:1.8.0_152]
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.Arrays.sort(Arrays.java:1506) ~[?:1.8.0_152]
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.ArrayList.sort(ArrayList.java:1462) ~[?:1.8.0_152]
   2017-11-28T15:02:05.60+0530 [APP/PROC/WEB/0] OUT     at java.util.Collections.sort(Collections.java:141) ~[?:1.8.0_152]

Implementation of MyClass

public class MyClass implements Comparable<MyClass>{

    private String title;
    private String description;
    private Long timeInMills;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getTimeInMills() {
        return timeInMills;
    }

    public void setTimeInMills(Long timeInMills) {
        this.timeInMills = timeInMills;
    }

    @Override
    public int compareTo(MyClass o) {
        return (int)(this.getTimeInMills() - o.getTimeInMills());
    }

}

I have seen many answers in the previous posts but none of them helped. can someone let me know the issue with my compareTo function.

Minions
  • 1,273
  • 1
  • 11
  • 28

1 Answers1

0

Following is quote from Oracle Javadoc page:

It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.

For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective.

So, check if your class is full filling this contract.

The problem is with precision loss when converting from Long to int. You can check at your end that if an object x and y of your class follow following:

If x.equals(y) is true then y.equals(x) is also true and vice versa.

Community
  • 1
  • 1
cse
  • 4,066
  • 2
  • 20
  • 37