【发布时间】:2014-08-17 11:43:22
【问题描述】:
我尝试使用以下代码对列表进行排序:
List<Image> sortedImages = new LinkedList<Image>(inputImages);
Collections.sort(sortedImages, imageScoreComparator);
和
class ImageScoreComparator implements Comparator<Image> {
ImageScoreCalculator imageScoreCalculator;
ImageScoreComparator() {
this(new ImageScoreCalculator(Calendar.getInstance()));
}
@VisibleForTesting
ImageScoreComparator(ImageScoreCalculator imageScoreCalculator) {
this.imageScoreCalculator = imageScoreCalculator;
}
@Override
public int compare(Image image1, Image image2) {
if (image2.isPromoted() && !image1.isPromoted()) {
return 1;
} else if (image1.isPromoted() && !image2.isPromoted()) {
return -1;
} else {
return Double.compare(imageScoreCalculator.getScoreForImage(image1), imageScoreCalculator.getScoreForImage(image2));
}
}
}
但我收到此错误:
java.lang.ClassCastException: com.MyComp.model.Image cannot be cast to java.lang.Comparable
这就是我初始化imageScoreComparator的方式
imageScoreComparator = mock(ImageScoreComparator.class);
when(imageScoreComparator.compare(image1, image2)).thenReturn(0);
【问题讨论】:
-
发布完整的堆栈跟踪。
标签: java sorting collections guava