java 比较不同的选项

发布于 2024-12-05 20:06:09 字数 152 浏览 1 评论 0原文

我已经实现了compareTo,允许我根据某些标准比较我的班级,并且工作正常。

但是,在某些时候,我想比较一件事上的类,而在代码中的另一点上,我想根据另一件事比较类。

是否可以有两种不同的compareTo 实现,并在某个时刻使用一个,在另一个时刻使用一个?

I have implemented compareTo to allow me to compare my class' based on some criteria and it is working fine.

However, at some point I want to compare the class' on one thing and at another point in the code I want to compare the class based on another thing.

Is it possible to have two different implementations of compareTo and using one at some point and one at another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷夜 2024-12-12 20:06:09

一般来说,执行此操作的机制是实现一个或多个 比较器并根据需要使用适当的比较器。

In general the mechanism to do this is to implement one or more Comparators and use the appropriate one as needed.

梦幻的味道 2024-12-12 20:06:09

由于您的类是“可比较的”,您可以使用compareTo,但是您不能创建该函数的多个实现以在同一类的不同点使用(您有一个要重写的函数,并且您可以'不要这样做两次)。

但是,您可以查看比较器接口< /a>;该接口的实现可以让您为您的对象实现和使用不同的compareTo。

Since your Class is "Comparable" you can use the compareTo, you can't - however - create more then one implementation of that function to be used at different points in the same Class (you have one function to override, and you can't do that twice).

You can, however, take a look at the Comparator Interface; and implementation of that interface can allow you to implement and use a different compareTo for your object.

听,心雨的声音 2024-12-12 20:06:09

我们通过为我们的类编写一个实用程序比较器来实现类似的目标 - 像这样:

public class FooComparator implements Comparator<Foo> {

    public static String COMPARE_FIELD1 = "COMPARE_FIELD1";
    public static String COMPARE_FIELD2 = "COMPARE_FIELD2";
    public static String COMPARE_FIELD3 = "COMPARE_FIELD3"; 
    private String compareBy = COMPARE_FIELD1;
    private boolean reverse = true;

    public FooComparator(){}

    public FooComparator(String sort){
        compareBy = sort;
    }

    public void reverse() {
        if(reverse) {reverse = false;
        } else {reverse = true;}
    }

    public void field1Sort() {compareBy = COMPARE_FIELD1;}
    public void field2Sort() {compareBy = COMPARE_FIELD2;}
    public void field3Sort() {compareBy = COMPARE_FIELD3;}

    public int compare(Foo foo1, Foo foo2) {
        if(compareBy.equals(COMPARE_FIELD2)) {
            return compareByField2(foo1, foo2);
        } else if(compareBy.equals(COMPARE_FIELD3)) {
            return compareByField3(foo1, foo2);
        } 
        return compareByField1(foo1, foo2);
    }

    private int compareByField1(Foo foo1, Foo foo2) {       
        if(reverse) {return foo1.getField1().compareTo(foo2.getField1());}
        return foo1.getField1().compareTo(foo2.getField1());        
    }

    private int compareByField2(Foo foo1, Foo foo2) {       
        if(reverse) {return foo1.getField2().compareTo(foo2.getField2());}
        return foo1.getField2().compareTo(foo2.getField2());        
    }

    private int compareByField3(Foo foo1, Foo foo2) {       
        if(reverse) {return foo1.getField3().compareTo(foo2.getField3());}
        return foo1.getField3().compareTo(foo2.getField3());        
    }   
}

然后我们可以像这样使用它:

List<Foo> foos = new ArrayList<Foo>();
FooComparator comparator = new FooComparator(FooComparator.COMPARE_FIELD1);     
Collections.sort(foos, comparator);

We achieved something similar by writing a utility comparator for our class - something like this:

public class FooComparator implements Comparator<Foo> {

    public static String COMPARE_FIELD1 = "COMPARE_FIELD1";
    public static String COMPARE_FIELD2 = "COMPARE_FIELD2";
    public static String COMPARE_FIELD3 = "COMPARE_FIELD3"; 
    private String compareBy = COMPARE_FIELD1;
    private boolean reverse = true;

    public FooComparator(){}

    public FooComparator(String sort){
        compareBy = sort;
    }

    public void reverse() {
        if(reverse) {reverse = false;
        } else {reverse = true;}
    }

    public void field1Sort() {compareBy = COMPARE_FIELD1;}
    public void field2Sort() {compareBy = COMPARE_FIELD2;}
    public void field3Sort() {compareBy = COMPARE_FIELD3;}

    public int compare(Foo foo1, Foo foo2) {
        if(compareBy.equals(COMPARE_FIELD2)) {
            return compareByField2(foo1, foo2);
        } else if(compareBy.equals(COMPARE_FIELD3)) {
            return compareByField3(foo1, foo2);
        } 
        return compareByField1(foo1, foo2);
    }

    private int compareByField1(Foo foo1, Foo foo2) {       
        if(reverse) {return foo1.getField1().compareTo(foo2.getField1());}
        return foo1.getField1().compareTo(foo2.getField1());        
    }

    private int compareByField2(Foo foo1, Foo foo2) {       
        if(reverse) {return foo1.getField2().compareTo(foo2.getField2());}
        return foo1.getField2().compareTo(foo2.getField2());        
    }

    private int compareByField3(Foo foo1, Foo foo2) {       
        if(reverse) {return foo1.getField3().compareTo(foo2.getField3());}
        return foo1.getField3().compareTo(foo2.getField3());        
    }   
}

We then can use it like this:

List<Foo> foos = new ArrayList<Foo>();
FooComparator comparator = new FooComparator(FooComparator.COMPARE_FIELD1);     
Collections.sort(foos, comparator);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文