简体   繁体   English

如何使关联对象仅可通过一种通用参数类型进行比较

[英]How to make an object of association comparable by only one generic parameter type

I'm trying to understand what <K, V extends Comparable<? super V>> 我试图了解什么<K, V extends Comparable<? super V>> <K, V extends Comparable<? super V>> actually means. <K, V extends Comparable<? super V>>实际上是指。

If compareTo() is called to compare two Association<K,V> objects; 如果调用compareTo()来比较两个Association<K,V>对象; which of the two generic types K or V will be used to make the comparison? 将使用两种通用类型KV的哪一个进行比较?

What will happen when <K, V extends Comparable<? super K>> <K, V extends Comparable<? super K>>会发生什么<K, V extends Comparable<? super K>> <K, V extends Comparable<? super K>> is used instead? <K, V extends Comparable<? super K>>代替吗?

 public class Association<K, V extends Comparable<? super V>> { K key; V value; //--------------------------------------- public int compareTo(Association<K, V> object) { return this.key.compareTo(object.key); } } 

edited the compareTo() method. 编辑了compareTo()方法。

If compareTo() is called to compare two Association objects; 如果调用compareTo()来比较两个Association对象; which of the two generic types K or V will be used to make the comparison? 将使用两种通用类型K或V中的哪一个进行比较?

As Association is implemented right now, neither will be used. 由于Association目前已实施,因此将不再使用。 Instead, your compareTo method will always throw a StackOverflowError , because it's incorrectly defined. 相反,您的compareTo方法将始终抛出StackOverflowError ,因为定义不正确。

(The implication is that it's the responsibility of the Association class and its definition of compareTo to decide what the answer to your question is. For example, you might write public int compareTo(Association<K, V> other) { return value.compareTo(other.value); } . ) (这暗示着Association类的责任及其compareTo定义来决定问题的答案是什么。例如,您可以编写public int compareTo(Association<K, V> other) { return value.compareTo(other.value); }

First to explain your current definition: 首先解释一下您当前的定义:

public class Association<K, V extends Comparable<? super V>>

This means you are defining a public class called Association This class has 2 type parameters: K and V . 这意味着您正在定义一个称为Association的公共类。该类有2个类型参数: KV Of these, V has the additional restraint that it must be comparable to ? super V 其中, V还有一个限制,那就是它必须与? super V相当? super V ? super V (= V or a super type of V ). ? super V (= V或超型的V )。

Since you want ot compare by the key, it should be K that is Comparable . 由于您希望通过键进行比较,因此应该是KComparable Additionally, you want to compare the Association s themselves, so it too must be Comparable . 另外,您想比较Association本身,因此它也必须是Comparable

So change your class definition to the following: 因此,将您的类定义更改为以下内容:

public class Association<K extends Comparable<K>, V> implements Comparable<Association<K, V>>

Your compareTo would then be: 您的compareTo将是:

public int compareTo(Association<K, V> other) {
    return key.compareTo(other.key);
}

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

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