I am in the process of upgrading our project to java 7. I ran into the Illegal Argument exception for Collections.sort(). I know the cause of the exception is the new Timsort in java 7 (I did go throw though all the questions raised previously on this issue). Now I need to modify the compare logic to overcome the exception. Here is my compare method
if (o1.isLookup() && !o2.isLookup()) {
return -1;
}
if (!o1.isLookup() && o2.isLookup()) {
return 1;
}
if (o1.dependsOn(o2)) {
return 1;
}
if (o2.dependsOn(o1)) {
return -1;
}
return 0;
I tried overriding the equals() method , with the same logic as compare, thinking if equals and compare return the same result it should resolve the issue; but it didn't work as expected .
When I split the compare method into two with separate comparators as shown below then the sort(using both comparators) does not throw any exception. What could be the possible reason?
Code Below:
protected Comparator<EntityWrapper> getComparator2() {
return new Comparator<EntityWrapper>() {
public int compare(EntityWrapper o1, EntityWrapper o2) {
if (o1.dependsOn(o2.entityClass)) {
// This depends on otherWrapper
return 1;
}
if (o2.dependsOn(o1.entityClass)) {
// OtherWrapper depends on this
return -1;
}
return 0;
}
};
}
protected Comparator<EntityWrapper> getComparator1() {
return new Comparator<EntityWrapper>() {
public int compare(EntityWrapper o1, EntityWrapper o2) {
if (o1.isLookup() && !o2.isLookup()) {
return -1;
}
if (!o1.isLookup() && o2.isLookup()) {
return 1;
}
return 0;
};
}