创建一个compareTo到实现Comparable的通用类
我有一个带有两个类型变量的通用类,它实现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因此,总结上面所说的并将其拼凑成一个工作代码是:
so to summarize the said above and to puzzle it together into a working code this is:
您想引入一个要求,即
K
和J
具有可以使用的自然顺序吗?在这种情况下,您可以像这样声明您的类DoubleKey
:然后您可以根据需要定义 DoubleKey 的
compareTo
。您可以执行以下操作:不过,您无法将
K
的任何实例与J
的实例进行比较。这些类型没有定义顺序。如果这些类型不一定具有自然顺序(许多类型没有),您可以将
Comparator
和Comparator
作为构造函数的参数您的DoubleKey
。 Google Guava 优秀的 Maps 类(具体参见newTreeMap
方法以及它们接受的类型的范围)。Would you like to introduce a requirement that
K
andJ
have a natural ordering that you can use? In this case you can declare your classDoubleKey
like this:You can then define your DoubleKey's
compareTo
as you like. You can do things like:You can't compare any instance of
K
to an instance ofJ
, 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>
andComparator<J>
as parameters to the constructor of yourDoubleKey
. A class that does this already that you can use as an example is Google Guava's excellent Maps class (see specifically thenewTreeMap
methods and the bounds of the types they accept).当
DoubleKey
小于、大于或等于此规则时,您必须定义一条规则。这就是比较的作用。也许,这是我的实际猜测,与DoubleKey的实例进行比较没有多大意义。
如果您实际上并不关心它们的排序方式,而只需要实现任何排序,请尝试以下操作:
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 ofDoubleKey<K,J>
.If you don't actual care how they're ordered and only need to implement any ordering, try this:
第一种方法:使用 hashCodes,例如
(你应该更多地考虑公式)
第二种方法:
将比较器添加到构造函数
First way: use hashCodes, like
(you should think more about formula)
Second way:
add comparator to constructor
通常情况下,有一个库可以解决您的问题: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.