简体   繁体   English

Java compareTo() 方法的可比、多种/不同实现

[英]Java Comparable, mutliple/different implenetations of compareTo() method

I have this class:我有这个 class:

public class Sample implements Comparable<Sample> {
public String a;
public String b;
public String c;

public int compareTo (Sample sampleToCompare) {
int compResult = this.a.compareTo(sampleToCompare.a);
      return (compResult != 0 ? compResult : 
                   this.b.compareTo(sampleToCompare.b));    
    }
}

I want compareTo() to behave or sort using different class properties depending if a flag is set.我希望compareTo()使用不同的 class 属性进行行为或排序,具体取决于是否设置了标志。

So, if flag == 1 I'd like compareTo() to using property c , otherwise is flag == 0 , whatever is currently in the method.因此,如果flag == 1我希望compareTo()使用属性c ,否则为flag == 0 ,无论当前在方法中。

In other words, sort the same class in different ways.换句话说,以不同的方式对相同的 class 进行排序。

I am not sure how to achieve this.我不确定如何实现这一目标。 Please help.请帮忙。

Also, please let me know if more information is needed from my side.另外,如果我需要更多信息,请告诉我。

If you want to implement different kind of sorting, you should take a look at java.util.Comparator interface.如果你想实现不同类型的排序,你应该看看java.util.Comparator接口。

public class SampleComparatorA implement Comparator<Sample> {

    public int compare(Sample a, Sample b) {
        // Your sorting
    }
}

And use java.util.Collections.sort() method with the Comparator as the secound parameter instead.并使用java.util.Collections.sort()方法将 Comparator 作为第二个参数。

Collections.sort(aSampleList, new SampleComparatorA());

How about:怎么样:

public int compareTo(Sample sampleToCompare) {
    if (flag == 1) {
        return this.c.compareTo(sampleToCompare.c);
    }
    if (flag == 0) {
        // current stuff
    }
    ...
}

That's not a very object-oriented way to do it, though.不过,这不是一种非常面向对象的方式。 Probably you should have two different comparators and a way to select them based on your "flag" value.可能你应该有两个不同的比较器和一个方法来 select 根据你的“标志”值。 Something like:就像是:

class Sample {
    private String a;
    private String b;
    private String c;
}

class ASampleComparator implements Comparator<Sample> {
    public int compare(Sample o1, Sample o2) {
        return o1.a.compareTo(o2.a);
    }
}

class BSampleComparator implements Comparator<Sample> {
    public int compare(Sample o1, Sample o2) {
        return o1.b.compareTo(o2.b);
    }
}

class CSampleComparator implements Comparator<Sample> {
    public int compare(Sample o1, Sample o2) {
        return o1.c.compareTo(o2.c);
    }
}

public Comparator<Sample> pickComparator(int flag) {
    switch (flag) {
        case 0:
            return new ASampleComparator();
        case 1:
            return new BSampleComparator();
        case 2:
            return new CSampleComparator();
        default:
            throw new IllegalArgumentException("Bad flag value: " + flag);
    }
}

You should make your flag static so the comparison will be consistent (as described in Effective Java, item 12 ), otherwise, you might get that a.compareTo(b) returns that a > b , but b.compareTo(a) returns that b > a .您应该设置您的标志 static 以便比较是一致的(如有效 Java,第 12 项中所述),否则,您可能会得到a.compareTo(b)返回a > b ,但b.compareTo(a)返回b > a So the simplest implementation I can think about is:所以我能想到的最简单的实现是:

public class Sample implements Comparable<Sample> {
public String a;
public String b;
public String c;
public static boolean my_flag = false;

public int compareTo (Sample sampleToCompare) {
    if (flag) {
        return this.c.compareTo(sampleToCompare.c);
    }
    int compResult = this.a.compareTo(sampleToCompare.a);
      return (compResult != 0 ? compResult : 
                   this.b.compareTo(sampleToCompare.b));    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM