Collections.sort()使用比较?

问题描述:

import java.util.*;

public class C_2 {
    public static void main(String args[]) {
        String theStrings[] = { "x", "a", "b", "c", "d" };
        List l = Arrays.asList(theStrings);
        Collections.sort(l);                            // line a
        Collections.sort(l, new ThisIsMyThing());       // line b
        System.out.println(l);
    }
}

class ThisIsMyThing implements Comparator {
    public int compare(Object o1, Object o2) {
        String s1 = (String)o1;
        String s2 = (String)o2;

        return -1 * s1.compareTo(s2);
    }
}



我理解类 C_2 根据两种不同的技术进行排序。
一个是标准 Collections.sort(l);
另一个是 Collections.sort(l,Comparator< >()); 我不能理解这种排序方法。有人可以向我解释吗?

I understand that class C_2 does sorting based on two different techniques. One is the standard Collections.sort(l); And the other is Collections.sort(l,Comparator<>()); I am not able to understand this sort method. Can someone please explain it to me?

Collection.sort(l)假设 l 的内容为 Comparable Collection.sort(1,Comparator)使用自定义比较器来比较 l 的内容,这是您做的。排序的概念(包括 sort()方法)意味着对象必须是可比的 - 在这种情况下,使用 Comparable Comparator

Collection.sort(l) assumes that the contents of l are Comparable. Collection.sort(1, Comparator) uses a custom comparator to compare the contents of l, this is what you did. The very idea of sorting (including the sort() method) implies the objects MUST be comparable - in this case, with either Comparable or Comparator.

请注意,许多Java对象都是可比较的,包括字符串日期数字。对于那些,你可以使用 Collection.sort(someList);

Note that many Java objects are comparable already, including String, Date and Number. For those, you can just use Collection.sort(someList);

假设您有圈子

public class Circle {
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getArea(){
        return radius * radius * Math.PI;
    }
}

如果您创建了100 Circle objects:

If you created 100 Circle objects:

ArrayList<Circle> circleList = new ArrayList<>();

for (int i = 0; i < 100; i++) {
    // adds a circle with random radius
    circleList.add(new Circle((int)(Math.random() * 100)));
}

// try to sort the list
Collections.sort(circleList);   //compilation error: must be Comparable

您不能对它们进行排序,因为Java不知道如何比较它们。你必须告诉这个Java:

You can't sort them because Java has no idea how to compare them. You have to tell this to Java:

public class Circle implements Comparable<Circle> {
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    // you MUST override the compareTo method from the Comparable interface
    @Override
    public int compareTo(Circle cirlce){
        if (this.getArea() > circle.getArea())
            return 1;
        else if (this.getArea() == circle.getArea())
            return 0;
        else 
            return -1;
    }

    public double getArea(){
        return radius * radius * Math.PI;
    }
}



With the compareTo() method in the Circle class, Java now knows how to compare them and can sort them.

现在你可以这样做:

Collections.sort(circleList);
// Yayyy I'm being sorted by the size of my areas!!!!!