1

通常我定义Comparator为 -

Comparator<Obj> comparator= new Comparator<Obj>() {
    public int compare(Obj o1, Obj o2) { 
        //implementation...
    } 
};

当它Comaprator是类成员的一部分时,它在代码格式上看起来不太好。有什么方法可以定义Comparatoras并在另一部分Comparator<Obj> comparator= new Comparator<Obj>()实现他的?compare

4

5 回答 5

1

只需定义一个class实现Comparator接口,并compare在该类中实现该方法,使用您的comparison logic: -

public class ObjComparator implements Comparator<Obj> {
    public int compare(Obj o1, Obj o2) { 
        //implementation
    }
}

然后,无论你想在哪里使用你的Comparator,使用这个类的一个实例。例如,在Arrays.sort: -

Obj[] yourArray = new Obj[10];
Arrays.sort(yourArray, new ObjComparator());
于 2012-11-26T10:02:29.470 回答
1

您可以有一个单独的类来实现Comparator

public class MyComapartor implements Comparator{

    public int compare(Obj o1, Obj o2) 
        { //implementation...
        } 
}

然后在你的课堂上使用它:

public class MyClass{

MyComapartor comp = new MyComapartor();

}
于 2012-11-26T10:03:56.900 回答
1

试试这个代码...

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class HDTV {
    private int size;
    private String brand;

    public HDTV(int size, String brand) {
        this.size = size;
        this.brand = brand;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }
}

class SizeComparator implements Comparator<HDTV> {
    @Override
    public int compare(HDTV tv1, HDTV tv2) {
        int tv1Size = tv1.getSize();
        int tv2Size = tv2.getSize();

        if (tv1Size > tv2Size) {
            return 1;
        } else if (tv1Size < tv2Size) {
            return -1;
        } else {
            return 0;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        HDTV tv1 = new HDTV(55, "Samsung");
        HDTV tv2 = new HDTV(60, "Sony");
        HDTV tv3 = new HDTV(42, "Panasonic");

        ArrayList<HDTV> al = new ArrayList<HDTV>();
        al.add(tv1);
        al.add(tv2);
        al.add(tv3);

        Collections.sort(al, new SizeComparator());
        for (HDTV a : al) {
            System.out.println(a.getBrand());
        }
    }
}
于 2014-04-11T12:16:40.060 回答
0

试试这个代码............

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Car {

    private String name;
    private String brand;
    private double cost;

    public Car(String name, String brand, double cost) {

        this.name = name;
        this.brand = brand;
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public String toString() {

        return getName();
    }

}

public class Hog {

    ArrayList<Car> cars = new ArrayList<Car>();

    public void setIt() {

        cars.add(new Car("Padmini", "Fiat", 100008.00));
        cars.add(new Car("XYlo", "Mahindra", 100000.00));
        cars.add(new Car("Swift", "Maruti", 200000.00));
    }

    public void sortIt() {

        Collections.sort(cars, new NameComparator());
        System.out.println(cars);
        Collections.sort(cars, new BrandComparator());
        System.out.println(cars);
        Collections.sort(cars, new CostComparator());
        System.out.println(cars);
    }

    class NameComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {

            return c1.getName().compareTo(c2.getName());
        }
    }

    class BrandComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {

            return c1.getBrand().compareTo(c2.getBrand());
        }
    }

    class CostComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {

            return new Double(c1.getCost()).compareTo(new Double(c2.getCost()));
        }
    }

    public static void main(String[] args) {

        Hog h = new Hog();

        h.setIt();
        h.sortIt();
    }

}
于 2012-11-26T10:01:40.337 回答
0

您是否尝试过使用嵌套类,例如:

public class Sample {
    public void method() {
        ObjComparator comparator = new ObjComparator();
    }
    public static class ObjComparator implements Comparator {
        public int compare(Obj o1, Obj o2) {
        }
    }
}

这是您寻求的解决方案吗?

于 2012-11-26T10:03:19.730 回答