assume the following java method:
private Collection<TimeDTO> sortTimes(Choice choice) {
final List<TimeDTO> times = new ArrayList<>(timeCache.getAll(choice));
Collections.sort(times, new Comparator<TimeDTO>() {
@Override
public int compare(final TimeDTO o1, final TimeDTO o2) {
if (o1.getTime() == null) {
return -1;
}
if (o2.getTime() == null) {
return 1;
}
return o1.getTime().compareTo(o2.getTime());
}
});
return times;
}
The List can contains large amount of items (50000+). Consider it as a multi-thread environment. getTime() return "org.joda.time.DateTime". timeCache can change on other threads.
But sometimes still got:
Caused by: java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:899) ~[na:1.8.0_301]
at java.util.TimSort.mergeAt(TimSort.java:516) ~[na:1.8.0_301]
at java.util.TimSort.mergeCollapse(TimSort.java:439) ~[na:1.8.0_301]
at java.util.TimSort.sort(TimSort.java:245) ~[na:1.8.0_301]
at java.util.Arrays.sort(Arrays.java:1512) ~[na:1.8.0_301]
at java.util.ArrayList.sort(ArrayList.java:1464) ~[na:1.8.0_301]
at java.util.Collections.sort(Collections.java:177) ~[na:1.8.0_301]
at ****.ServiceImpl.sortTimes(ServiceImpl.java:100)
What can be the problem? Any idea? Thank you.