java - 使用比较器进行集合排序,给出 IllegalArgumentException

标签 java android

在这里,我根据距当前位置的距离找出实体的所有排序列表。 当我用任何常量值代替 ebeeRoutePlansList.get(i).getRetailerLatitude() 时, collection.sort 就能正常工作。我的数据在纬度或其他方面有什么问题吗?这是我的API的链接http://52.74.125.208:9080/routeplanuser/getUserVisitDetails/1 从这里我们获取纬度和经度并保存在 EbeeRouteplanlist

 for(int i = 0; i < ebeeRoutePlansList.size(); i++)
        {
            double dLat = Math.toRadians(currentLatitute-ebeeRoutePlansList.get(i).getRetailerLatitude());
            double dLng = Math.toRadians(currentlongitude-ebeeRoutePlansList.get(i).getRetailerLongitude());
            double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.cos(Math.toRadians(28.459267)) * Math.cos(Math.toRadians(currentLatitute)) *
                            Math.sin(dLng/2) * Math.sin(dLng/2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
             dist =  (float)(earthRadius * c);
            Log.d(TAG,"the distance of each lat Long ==="+dist+"name "+ebeeRoutePlansList.get(i).getRetailerName());
            ebeeRoutePlansList.get(i).setShortedDistance(dist);

        }

我使用了Collections.sort(ebeeRoutePlansList, new DistanceComparator());

DistanceComparator 类具有

class DistanceComparator implements Comparator<EbeeRoutePlan> {
        @Override
        public int compare(EbeeRoutePlan a, EbeeRoutePlan b) {
            Log.d(TAG,"the compare method inside Distance Comparator "+a.getShortedDistance()+"  --- "+b.getShortedDistance());

            if(a.getShortedDistance() == null || b.getShortedDistance() == null)
            {
                return 0;
            }

            return a.getShortedDistance() < b.getShortedDistance() ? -1 : a.getShortedDistance() == b.getShortedDistance() ? 0 : 1;
        }
    } 

首先打印这个输出

12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.542  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.552  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 0.09162273
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 0.09162273
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 0.09162273
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 0.09162273
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 0.09162273
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 0.09162273
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 0.09162273
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.562  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 0.09162273
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273  --- 8.636234
12-07 21:37:20.572  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234  --- 8.636234

然后在执行最后一行后发生异常

12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ java.lang.IllegalArgumentException: Comparison method violates its general contract!
12-07 21:37:20.582  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.mergeLo(TimSort.java:743)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.mergeAt(TimSort.java:479)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.mergeCollapse(TimSort.java:406)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.sort(TimSort.java:210)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.sort(TimSort.java:169)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.Arrays.sort(Arrays.java:2038)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.Collections.sort(Collections.java:1891)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.ebee.mobile.nativeandroid.activities.cartpage.CartPageActivity.getSortedDistanceRetailerList(CartPageActivity.java:853)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.ebee.mobile.nativeandroid.activities.cartpage.CartPageActivity$1.run(CartPageActivity.java:188)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.os.Looper.loop(Looper.java:176)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5317)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-07 21:37:20.592  25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

你能帮我解决这个问题吗

最佳答案

a.getShortedDistance() 的数据类型是什么?是双的吗?比较 double 是否相等是一个错误,你不应该这样做,

a.getShortedDistance() ==b.getShortedDistance()

。将第二个内部条件替换为 ,

a.getShortedDistance() > b.getShortedDistance()?1:0

此外,您还会遇到异常,因为方法违反了传递性。可以说 a.getShortedDistance()= nullb.getShortedDistance() = 10c.getShortedDistance()=20。你的方法使 a=b 和 a=c 但 b != c 这是错误的。如果a==b且b!=c则a!=c。

我想,你正确的方法应该简单地做到这一点,

return a.getShortedDistance() < b.getShortedDistance() ? -1 : a.getShortedDistance() > b.getShortedDistance() ? 1 : 0;

希望有帮助。

关于java - 使用比较器进行集合排序,给出 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138119/

相关文章:

java - 检查字符串是否为空

java - 如何检查 ElasticSearch 的恢复是否完成?

Java 可重入锁和条件 |生产者完成工作,消费者陷入困境

java - Android上使用SoftReference缓存Bitmap导致OOM

android - 不能在约束中表示 214748364 的大小

android - 批量更新数据库

java - 在java中使用InputStream和OutputStream传输时图像不显示

android - 如何将整数 ArrayList 添加到 MultipartBody

android - 我可以使用 Phonegap/cordova 框架复制目录吗?

android - 在 ListView 中创建数组