【问题标题】:Collection sort throws exception [duplicate]集合排序引发异常[重复]
【发布时间】:2019-07-05 08:48:55
【问题描述】:

我正在尝试对我的用户分数进行排序。它在本地运行良好,但是当我查看服务器日志时,它会引发很多异常。所以我尝试空检查并捕获异常,但异常仍然抛出异常。我不确定有什么例外。

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]

【问题讨论】:

    标签: java spring scala spring-boot kotlin


    【解决方案1】:

    无论传递给重写的compare 方法的参数的顺序如何,它都会给出相同的结果。假设我们有o1.getScore().doubleValue() == o2.getScore().doubleValue()。然后compare(o1, o2) == 1 &amp;&amp; compare(o2, o1) == 1compare 的约定是对于每对参数 o1o2,以下必须为真:compare(o1, o2) + compare(o2, o1) == 0

    【讨论】:

      【解决方案2】:

      您的 Comparator 没有正确处理将两个 null 视为相等的情况。如果您比较两个对象o1o2,它们都是null,您的代码将导致:

      compare(o1,o2) == 1

      compare(o2,o1) == 1

      这应该是不可能的,因为o1 不能同时大于小于o2。因此,您看到的错误。

      如果o1o2(或它们各自的getScore() 方法)都为空,您需要修改代码以返回0

      【讨论】:

        猜你喜欢
        • 2010-12-13
        • 1970-01-01
        • 2021-06-16
        • 2020-12-30
        • 2011-11-28
        • 2021-10-10
        • 2012-08-21
        • 1970-01-01
        • 2015-04-17
        相关资源
        最近更新 更多