我正在尝试对用户分数进行排序。它在本地运行良好,但是当我查看服务器日志时会抛出很多异常。因此,我尝试使检查无效并捕获异常,但异常仍会引发异常。我不确定有什么例外。
Collections.sort(userScores, new Comparator<UserScore>() {
@Override
public int compare(UserScore o1, UserScore o2) {
try {
if(o1 == null || o1.getScore() == null) {
return 1;
}
if(o2 == null || o2.getScore() == null) {
return -1;
}
if(o1.getScore().doubleValue() > o2.getScore().doubleValue())
return -1;
return 1;
}catch(Exception ex) {
logger.severe(ex.getMessage());
return 0;
}
}
});
我的代码抛出了,
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:777) ~[na:1.8.0_181]
at java.util.TimSort.mergeAt(TimSort.java:514) ~[na:1.8.0_181]
at java.util.TimSort.mergeCollapse(TimSort.java:441) ~[na:1.8.0_181]
at java.util.TimSort.sort(TimSort.java:245) ~[na:1.8.0_181]
at java.util.Arrays.sort(Arrays.java:1438) ~[na:1.8.0_181]
at java.util.List.sort(List.java:478) ~[na:1.8.0_181]
at java.util.Collections.sort(Collections.java:175) ~[na:1.8.0_181]
at com.tll.treeofwords.game.service.GameService.generateRanking(GameService.java:179) ~[classes!/:0.0.1-SNAPSHOT]
at com.tll.treeofwords.game.service.GameService.getCurrentGame(GameService.java:76) ~[classes!/:0.0.1-SNAPSHOT]
at com.tll.treeofwords.game.service.GameService.getCurrentGame(GameService.java:59) ~[classes!/:0.0.1-SNAPSHOT]
at com.tll.treeofwords.game.service.GameService$$FastClassBySpringCGLIB$$d5f3b9ef.invoke(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at com.tll.treeofwords.game.service.GameService$$EnhancerBySpringCGLIB$$38f6aa6d.getCurrentGame(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at com.tll.treeofwords.game.controller.GameController.getGame(GameController.java:59) ~[classes!/:0.0.1-SNAPSHOT]
at com.tll.treeofwords.game.controller.GameController$$FastClassBySpringCGLIB$$b9539785.invoke(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669) ~[spring-aop-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at com.tll.treeofwords.game.controller.GameController$$EnhancerBySpringCGLIB$$388a70ef.getGame(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.GeneratedMethodAccessor145.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
答案 0 :(得分:3)
您的比较器无法正确处理将两个null视为相等的情况。如果比较两个o1
的两个对象o2
和null
,则代码将得出:
compare(o1,o2) == 1
和也:
compare(o2,o1) == 1
这应该是不可能的,因为o1
不能同时大于o2
且小于<{1}}。因此,您看到的错误。
如果0
和o1
(或它们各自的o2
方法)都为空,则需要修改代码以返回getScore()
。
答案 1 :(得分:2)
无论将什么顺序的参数传递给重写的compare
方法,它都会给出相同的结果。假设我们有o1.getScore().doubleValue() == o2.getScore().doubleValue()
。然后compare(o1, o2) == 1 && compare(o2, o1) == 1
。 compare
的约定是,对于每对参数o1
和o2
,必须满足以下条件:compare(o1, o2) + compare(o2, o1) == 0
。