apache spark Java日期属性比较器契约冲突
我有一个对象数组列表,我想按日期降序排序(最近的第一个)。类Foo有一个字符串参数“createdAt”,我用来定义compare-to方法重写
“createdAt”可能为null、空白或无效日期
@Override
public int compareTo(Foo foo) {
Date created = new Date(0);
Date otherCreated = new Date(0);
try {
created = Utils.getFormattedDate(createdAt);
} catch (Exception e) {
e.printStackTrace();
}
try {
otherCreated = Utils.getFormattedDate(foo.createdAt);
} catch (Exception e) {
e.printStackTrace();
}
return Long.compare(otherCreated.getTime(),created.getTime());
}
对于空检查,我定义了一个比较器
public static final Comparator fooComparator = new Comparator<Foo>() {
@Override
public int compare(Foo o1, Foo o2) {
if (o1 ==null || o1.getCreatedAt() == null) {
return (o2 ==null || o2.getCreatedAt() == null) ? 0 : 1;
}
if (o2==null || o2.getCreatedAt() == null) {
return -1;
}
return o1.compareTo(o2);
}
};
我试着用
ArrayList <Foo> fooList = ....
Collections.sort(fooList, fooComparator);
但在spark上的MapReduce作业中运行此代码会得到“比较方法违反了其一般约定”
我试着写了很多可传递的测试用例,但都通过了。我错过了什么
这是我的测验
@org.junit.Test
public void testCompare_Contract() {
Foo t2 = new Foo();
t2.setId(2L);
t2.setCreatedAt(formatter.format(new Date(2018, 10, 28, 0, 0, 0).getTime()));
t2.setText("Foo 2");
Foo t3 = new Foo();
t3.setId(3L);
t3.setCreatedAt(null);
t3.setText("Foo 3");
Foo t4 = new Foo();
t4.setId(4L);
t4.setCreatedAt(formatter.format(new Date(2018, 10, 30, 0, 0, 0).getTime()));
t4.setText("Foo 4");
assertEquals(1, t3.compareTo(t2));
assertEquals(-1, t2.compareTo(t3));
assertEquals(1, t3.compareTo(t4));
assertEquals(-1, t4.compareTo(t3));
assertEquals(1, t2.compareTo(t4));
assertEquals(-1, t4.compareTo(t2));
assertEquals(0, t2.compareTo(t2));
}
共 (0) 个答案