致命异常:java.lang.IlLegalArgumentException:比较方法违反其常规合同

2022-08-22 00:00:00 collections android java comparator

我知道有很多类似的问题,我通过阅读这些问题的答案得到了很大的帮助,但我不能看到我的客户是如何面对这个问题的。而且只有一个客户端面临此问题。

我有一个列表,我正在使用比较器接口对该列表进行排序。您认为以下代码有问题吗?

    private static class BiologySamplesComparator implements Comparator<BiologySample>, Serializable {
        @Override
        public int compare(BiologySample left, BiologySample right) {
            if (left == right || (left != null && right != null && left.getSampleDateTime() == right.getSampleDateTime())) {
                return 0;
            }

            if (left == null || left.getSampleDateTime() == null) {
                return 1;
            }

            if (right == null || right.getSampleDateTime() == null) {
                return -1;
            }

            return right.getSampleDateTime().compareTo(left.getSampleDateTime());
        }
    }

我就是这样调用这个函数的

Collections.sort(biologySamples, new BiologySamplesComparator());

我知道这种情况下的主要问题是传递性。然而,我想不出是什么违反了这条规定。

这是如何getSampleDateTime()返回日期09星期五17:00:00 PDT 2021


更新 这就是我能够解决我的问题的方法。 我希望这会有帮助,我在这个问题上被困了很长时间。

    private static class BiologySamplesComparator implements Comparator<BiologySample>, Serializable {
        @Override
        public int compare(BiologySample left, BiologySample right) {
            if (left == null) {
                if (right == null) {
                    return 0;
                } else {
                    return 1;
                }
            } else if (right == null) {
                return -1;
            } else if (left == right) {
                return 0;
            }
            if (left.getSampleDateTime() == null) {
                if (right.getSampleDateTime() == null) {
                    return 0;
                } else {
                    return 1;
                }
            } else if (right.getSampleDateTime() == null) {
                return -1;
            } else if (left.getSampleDateTime() == right.getSampleDateTime()) {
                return 0;
            }

            return right.getSampleDateTime().compareTo(left.getSampleDateTime());
        }
    }

解决方案

我在某些情况下缺少一些条件,这就是我能够解决问题的方法。

    private static class BiologySamplesComparator implements Comparator<BiologySample>, Serializable {
        @Override
        public int compare(BiologySample left, BiologySample right) {
            if (left == null) {
                if (right == null) {
                    return 0;
                } else {
                    return 1;
                }
            } else if (right == null) {
                return -1;
            } else if (left == right) {
                return 0;
            }
            if (left.getSampleDateTime() == null) {
                if (right.getSampleDateTime() == null) {
                    return 0;
                } else {
                    return 1;
                }
            } else if (right.getSampleDateTime() == null) {
                return -1;
            } else if (left.getSampleDateTime() == right.getSampleDateTime()) {
                return 0;
            }

            return right.getSampleDateTime().compareTo(left.getSampleDateTime());
        }
    }

Why does my compare methd throw IllegalArgumentException sometimes?

提供

相关文章