Java ::引起:java.lang.IllegalArgumentException:比较方法违反了其一般合同

时间:2017-10-23 06:17:55

标签: java

FileStatus[] status = hdfs.listStatus(new Path(args[5] + "/" + typeOfFlow + "/" + args[4] + "/" + decileValue + "/"));

// you need to pass in your hdfs path
ArrayList<FileStatus> directories1 = new ArrayList<FileStatus>();
for (FileStatus f : status) {
    if (f.isDir()) {
        directories1.add(f);
    }
}
FileStatus[] directories = directories1.toArray(new FileStatus[directories1.size()]);
Arrays.sort(directories, new Comparator<FileStatus>() {
    public int compare(FileStatus o1, FileStatus o2) {
        return (int) (o1.getModificationTime() - o2.getModificationTime());
    }
});

我得到以下异常:

Caused by: java.lang.IllegalArgumentException: Comparison method violates its general contract!
                at java.util.TimSort.mergeHi(TimSort.java:899)
                at java.util.TimSort.mergeAt(TimSort.java:516)
                at java.util.TimSort.mergeForceCollapse(TimSort.java:457)
                at java.util.TimSort.sort(TimSort.java:254)
                at java.util.Arrays.sort(Arrays.java:1438)

我创建了一个测试类并检查了两个对象null或object.getModeificationTime null中的任何一个,但堆栈跟踪不相同:

Exception in thread "main" java.lang.NullPointerException
    at MyObject.getModificationTime(Main.java:45)
    at Main$1.compare(Main.java:35)
    at Main$1.compare(Main.java:33)
    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    at java.util.TimSort.sort(TimSort.java:220)
    at java.util.Arrays.sort(Arrays.java:1438) 

我想知道在第一次出现异常情况是什么情况。

0 个答案:

没有答案