创建一个compareTo到实现Comparable的通用类

发布于 2024-10-17 18:27:00 字数 684 浏览 7 评论 0原文

我有一个带有两个类型变量的通用类,它实现了 java.lang.Comparable。

public class DoubleKey<K,J> implements Comparable<DoubleKey<K,J>>{

    private K key1;
    private J key2;

    public DoubleKey(K key1, J key2){
        this.key1 = key1;
        this.key2 = key2;
    } 

    public K getFirstKey(){
        return this.key1;
    }

    public J getSecondKey(){
        return this.key2;
    }

    // need for Comparable interface
    public int compareTo(DoubleKey<K,J> aThat){
        ...
    }

}

因为我用 Comparable 实现了它,所以我需要编写compareTo() 方法。因为 K、J 可以是 ANY 类型,所以我在如何完全比较它们方面遇到了问题。有没有办法能够在比较中捕获所有可能的类型(Primitive、Wrapper、Object)?感谢您的帮助!

I have a Generic Class with two type variables, which implements java.lang.Comparable.

public class DoubleKey<K,J> implements Comparable<DoubleKey<K,J>>{

    private K key1;
    private J key2;

    public DoubleKey(K key1, J key2){
        this.key1 = key1;
        this.key2 = key2;
    } 

    public K getFirstKey(){
        return this.key1;
    }

    public J getSecondKey(){
        return this.key2;
    }

    // need for Comparable interface
    public int compareTo(DoubleKey<K,J> aThat){
        ...
    }

}

Becuase i implemeted it with Comparable, I need to write the compareTo() method. Because K, J can be of ANY type, I'm having problems on how to compare them completely. Is there a way to be able to catch all possible types (Primitive, Wrapper, Object) in the comparison? Thanks for the help!

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

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

发布评论

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

评论(6

秋叶绚丽 2024-10-24 18:27:00

因此,总结上面所说的并将其拼凑成一个工作代码是:

    public class DoubleKey<K extends Comparable<K>, J extends Comparable<J>>
        implements Comparable<DoubleKey<K, J>> {

    private K key1;
    private J key2;

    public DoubleKey(K key1, J key2) {
        this.key1 = key1;
        this.key2 = key2;
    }

    public K getFirstKey() {
        return this.key1;
    }

    public J getSecondKey() {
        return this.key2;
    }

    public int compareTo(DoubleKey<K, J> that) {

        int cmp = this.getFirstKey().compareTo(that.getFirstKey());
        if (cmp == 0)
            cmp = this.getSecondKey().compareTo(that.getSecondKey());
        return cmp;
    }
}

so to summarize the said above and to puzzle it together into a working code this is:

    public class DoubleKey<K extends Comparable<K>, J extends Comparable<J>>
        implements Comparable<DoubleKey<K, J>> {

    private K key1;
    private J key2;

    public DoubleKey(K key1, J key2) {
        this.key1 = key1;
        this.key2 = key2;
    }

    public K getFirstKey() {
        return this.key1;
    }

    public J getSecondKey() {
        return this.key2;
    }

    public int compareTo(DoubleKey<K, J> that) {

        int cmp = this.getFirstKey().compareTo(that.getFirstKey());
        if (cmp == 0)
            cmp = this.getSecondKey().compareTo(that.getSecondKey());
        return cmp;
    }
}
短叹 2024-10-24 18:27:00

您想引入一个要求,即 KJ 具有可以使用的自然顺序吗?在这种情况下,您可以像这样声明您的类 DoubleKey

class DoubleKey<K extends Comparable<K>, J extends Comparable<J>>

然后您可以根据需要定义 DoubleKey 的 compareTo。您可以执行以下操作:

getFirstKey().compareTo(aThat.getFirstKey())

不过,您无法将 K 的任何实例与 J 的实例进行比较。这些类型没有定义顺序。

如果这些类型不一定具有自然顺序(许多类型没有),您可以将 ComparatorComparator 作为构造函数的参数您的 DoubleKey。 Google Guava 优秀的 Maps 类(具体参见 newTreeMap 方法以及它们接受的类型的范围)。

Would you like to introduce a requirement that K and J have a natural ordering that you can use? In this case you can declare your class DoubleKey like this:

class DoubleKey<K extends Comparable<K>, J extends Comparable<J>>

You can then define your DoubleKey's compareTo as you like. You can do things like:

getFirstKey().compareTo(aThat.getFirstKey())

You can't compare any instance of K to an instance of J, though. There is no ordering defined over those types.

If these types don't necessarily have a natural ordering (many don't), you can take a Comparator<K> and Comparator<J> as parameters to the constructor of your DoubleKey. A class that does this already that you can use as an example is Google Guava's excellent Maps class (see specifically the newTreeMap methods and the bounds of the types they accept).

享受孤独 2024-10-24 18:27:00
public class DoubleKey<
        K implements Comparable<K>, 
        J implements Comparable<J>> 
    implements Comparable<DoubleKey<K,J>> {

    public int compareTo(DoubleKey<K,J> that){
        int cmp = this.key1.compareTo(that.key1);
        if(cmp==0) cmp = this.key2.compareTo(that.key2);
        return cmp;
    }
}
public class DoubleKey<
        K implements Comparable<K>, 
        J implements Comparable<J>> 
    implements Comparable<DoubleKey<K,J>> {

    public int compareTo(DoubleKey<K,J> that){
        int cmp = this.key1.compareTo(that.key1);
        if(cmp==0) cmp = this.key2.compareTo(that.key2);
        return cmp;
    }
}
非要怀念 2024-10-24 18:27:00

DoubleKey 小于、大于或等于此规则时,您必须定义一条规则。这就是比较的作用。也许,这是我的实际猜测,与 DoubleKey的实例进行比较没有多大意义。

如果您实际上并不关心它们的排序方式,而只需要实现任何排序,请尝试以下操作:

public int compareTo(DoubleKey<K,J> that){
    // real codes needs checks for null values!
    return (this.key1.toString() + this.key2.toString()).compareTo(that.key1.toString() + that.key2.toString());
}

You'll have to define a rule when a DoubleKey<K,J> is smaller, bigger or equal to this one. That's what compare does. Maybe, that's my actual guess, it doesn't make much sense to compare to instances of DoubleKey<K,J>.

If you don't actual care how they're ordered and only need to implement any ordering, try this:

public int compareTo(DoubleKey<K,J> that){
    // real codes needs checks for null values!
    return (this.key1.toString() + this.key2.toString()).compareTo(that.key1.toString() + that.key2.toString());
}
痴情换悲伤 2024-10-24 18:27:00

第一种方法:使用 hashCodes,例如

 public int compareTo(DoubleKey<K,J> aThat){
     getFirstKey().hashCode() + getSecondKey().hashCode() - aThat.getFirstKey().hashCode() +   aThat.getSecondKey().hashCode();
 }

(你应该更多地考虑公式)

第二种方法:
将比较器添加到构造函数

public DoubleKey(K key1, J key2, Comparator cmp){

First way: use hashCodes, like

 public int compareTo(DoubleKey<K,J> aThat){
     getFirstKey().hashCode() + getSecondKey().hashCode() - aThat.getFirstKey().hashCode() +   aThat.getSecondKey().hashCode();
 }

(you should think more about formula)

Second way:
add comparator to constructor

public DoubleKey(K key1, J key2, Comparator cmp){
蘑菇王子 2024-10-24 18:27:00

通常情况下,有一个库可以解决您的问题:Apache Commons lang3。我经常使用 Pair实例作为键。他们实施了可比性。

As is often the case, there exists a library that can solve your problem: Apache Commons lang3. I often use Pair<L,R> instances as keys. They implement Comparable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文