【问题标题】:Sorting with Comparator and Arrays.asList()使用 Comparator 和 Arrays.asList() 进行排序
【发布时间】:2015-07-17 09:10:04
【问题描述】:

当我执行下面的代码时,我得到的输出为[0, -2000000000, 2000000000]

import java.util.Arrays;
import java.util.Comparator;

public class SordidSort {
public static void main(String args[]) {
    Integer big = new Integer(2000000000);
    Integer small = new Integer(-2000000000);
    Integer zero = new Integer(0);
    Integer[] arr = new Integer[] { big, small, zero };
    Arrays.sort(arr, new Comparator<Object>() {
        public int compare(Object o1, Object o2) {
            return ((Integer) o2).intValue() - ((Integer) o1).intValue();
        }
    });
    System.out.println(Arrays.asList(arr));
    }
}

它是如何对数字进行排序的?

【问题讨论】:

标签: java arrays collections comparator


【解决方案1】:

代替

public int compare(Object o1, Object o2) {
    return ((Integer) o2).intValue() - ((Integer) o1).intValue();
}

使用下面的

public int compare(Object o1, Object o2) {
    int x1 = ((Integer) o1).intValue();
    int x2 = ((Integer) o2).intValue();
    if (x1 < x2) {
        return -1;
    } else if (x1 == x2) {
        return 0;
    } else {
        return 1;
    }
}

您的代码可能会产生溢出。产生溢出时会得到奇怪的顺序。

【讨论】:

  • 那么 ((Integer) o1).compareTo((Integer) o2) 呢?
  • 实际上根本不需要比较器。这是整数的自然顺序。这只是为了强调问题出在 int - int
【解决方案2】:

手动实现比较器有一些微妙之处。您刚刚了解了int 类型。正确比较两个 floatdouble 值要困难得多。如果您不处理-0.0,您可能有0.0-0.0 排序错误。而如果不处理NaN,可能会导致灾难(排序后随机排序,破TreeMap等)。幸运的是,在每种盒装类型中都有名为 compare 的现成静态方法:Integer.compare(从 Java 7 开始)、Double.compareFloat.compare 等等。使用它们,您将永远不会遇到这样的问题。

由于 Java 8 实现比较器要简单得多:您有现成的辅助方法,例如 Comparator.comparingInt:

Arrays.sort(arr, Comparator.comparingInt(o -> ((Integer)o).intValue()));

【讨论】:

    【解决方案3】:

    如下修改你的代码::

    import java.util.Arrays;
    import java.util.Comparator;
    
    public class SordidSort {
    
        public static void main(String args[]) {
            Integer big = new Integer(2000000000);
            Integer small = new Integer(-2000000000);
            Integer zero = new Integer(0);
            Integer[] arr = new Integer[] { big, small, zero };
            Arrays.sort(arr, new Comparator<Object>() {
                public int compare(Object o1, Object o2) {
    
                    int o1Val = ((Integer) o1).intValue();
                    int o2Val = ((Integer) o2).intValue();
    
                    if(o1Val > o2Val){
                        return 1;
                    } else if(o1Val == o2Val){
                        return 0;
                    }else{
                        return -1; 
                    }
                }
            });
            System.out.println(Arrays.asList(arr));
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以将您的 Comparator 声明为 new Comparator&lt;Integer&gt;(),以便将 Integers 传递给您的比较函数,然后您可以从 Integer.compareTo 方法中受益

      public static void main(String args[]) {
          Integer big = new Integer(2000000000);
          Integer small = new Integer(-2000000000);
          Integer zero = new Integer(0);
          Integer[] arr = new Integer[] { big, small, zero };
          Arrays.sort(arr, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
              return o1.compareTo(o2);
            }
          });
          System.out.println(Arrays.asList(arr));
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-09
        • 2019-04-07
        • 1970-01-01
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多