java.util.Collections#sort() throws IllegalArgumentException

问题

JDK6升级JDK7的情况下,由于java.util.Arrays.sort和java.util.Collections.sort使用的排序算法已被替换(mergesort → timsort)。当TimSort检测到待比较的项目违反约定,则可能抛出IllegalArgumentException。JDK6及以前的版本忽略了这种情况。如果需要以前的行为,可以使用新的系统属性java.util.Arrays.useLegacyMergeSort来恢复以前的mergesort行为。


    /**
     * 
     * @param <T> the class of the objects to be sorted
     * @param a the array to be sorted
     * @param c the comparator to determine the order of the array.  A
     *        {@code null} value indicates that the elements'
     *        {@linkplain Comparable natural ordering} should be used.
     * @throws ClassCastException if the array contains elements that are
     *         not <i>mutually comparable</i> using the specified comparator
     * @throws IllegalArgumentException (optional) if the comparator is
     *         found to violate the {@link Comparator} contract
     */
    public static <T> void sort(T[] a, Comparator<? super T> c) {
        if (c == null) {
            sort(a);
        } else {
            if (LegacyMergeSort.userRequested)
                legacyMergeSort(a, c);
            else
                TimSort.sort(a, 0, a.length, c, null, 0, 0);
        }
    }

Collections.sort方法的说明
简单的来说:

  1. 恒满足 compare(x, y) = - compare(y, x)compare(x, y) 抛出异常,compare(y, x)也必须抛出异常
  2. (compare(x, y)>0compare(y, z)>0),可以推导出 compare(x, z) > 0
  3. compare(x, y)==0,推导出对任意元素z,恒满足compare(x, z) = compare(y, z)

解决方案

设置JVM启动参数,使用JDK6的默认排序方法。

-Djava.util.Arrays.useLegacyMergeSort=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值