[英]Java Generics: compareTo and “capture#1-of ?”
The following gives me an error message: 以下给出了一条错误消息:
public static List<Comparable<?>> merge(Set<List<Comparable<?>>> lists) {
List<Comparable<?>> result = new LinkedList<Comparable<?>>();
HashBiMap<List<Comparable<?>>, Integer> location = HashBiMap.create();
int totalSize;
for (List<Comparable<?>> l : lists) {
location.put(l, 0);
totalSize += l.size();
}
boolean first;
List<Comparable<?>> lowest; //the list with the lowest item to add
int index;
while (result.size() < totalSize) {
first = true;
for (List<Comparable<?>> l : lists) {
if (! l.isEmpty()) {
if (first) {
lowest = l;
}
else if (l.get(location.get(l)).compareTo(lowest.get(location.get(lowest))) <= 0) { //error here
lowest = l;
}
}
}
index = location.get(lowest);
result.add(lowest.get(index));
lowest.remove(index);
}
return result;
}
The error is: 错误是:
The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments (Comparable<capture#2-of ?>)
What's going on here? 这里发生了什么? I made the type of everything Comparable
so I could call .compareTo
and sort this list. 我做了所有Comparable
的类型,所以我可以调用.compareTo
并对此列表进行排序。 Am I using generics incorrectly? 我是否错误地使用泛型?
List<?>
means "List of anything", so two objects with this type are not the same: One could be a list of String
, the other a list of BigDecimal
. List<?>
表示“任何列表”,因此这种类型的两个对象不相同:一个可以是String
列表,另一个可以是BigDecimal
列表。 Obviously, those are not the same. 显然,那些不一样。
List<T>
means "List of anything but when you see T
again, it's the same T
". List<T>
表示“除了再次看到T
的所有内容列表,它都是相同的T
”。
You must tell the compiler when you mean the same type in different places. 当你在不同的地方使用相同的类型时,你必须告诉编译器。 Try: 尝试:
public static <T extends Comparable<? super T>> List<T> merge(Set<List<T>> lists) {
List<T> result = new LinkedList<T>();
HashBiMap<List<T>, Integer> location = HashBiMap.create();
[EDIT] So what does <T extends Comparable<? super T>> List<T>
[编辑]那么<T extends Comparable<? super T>> List<T>
<T extends Comparable<? super T>> List<T>
mean? <T extends Comparable<? super T>> List<T>
是什么意思? The first part defines a type T
with the following properties: It must implement the interface Comparable<? super T>
第一部分定义了一个具有以下属性的类型T
:它必须实现接口Comparable<? super T>
Comparable<? super T>
(or Comparable<X>
where X
is also defined in terms of T
). Comparable<? super T>
(或Comparable<X>
,其中X
也以T
)。
? super T
? super T
means that the type which the Comparable
supports must T
or one of its super types. ? super T
表示Comparable
支持的类型必须为T
或其超类型之一。
Imagine for a moment this inheritance: Double extends Integer extends Number
. 想象一下这个继承: Double extends Integer extends Number
。 This is not correct in Java but imagine that Double
is just an Integer
plus a fraction part. 这在Java中是不正确的,但想象Double
只是一个Integer
加上一个小部分。 In this scenario, a Comparable
which works for Number
also works for Integer
and Double
since both derive from Number
. 在这种情况下,适用于Number
的Comparable
也适用于Integer
和Double
因为它们都来自Number
。 So Comparable<Number>
would satisfy the super
part for T
being Number
, Integer
or Double
. 因此, Comparable<Number>
将满足T
为Number
, Integer
或Double
的super
部分。
As long as each of these types support the Comparable
interface, they also satisfy the first part of the declaration. 只要这些类型中的每一个都支持Comparable
接口,它们也满足声明的第一部分。 This means, you can pass in Number
for T
and the resulting code will also work when there are Integer
and Double
instances in the lists. 这意味着,您可以传入Number
for T
并且当列表中存在Integer
和Double
实例时,生成的代码也将起作用。 If you Integer
for T
, you can still use Double
but Number
is not possible because it doesn't satisfy T extends Comparable
anymore (the super
part would still work, though). 如果你是T
Integer
,你仍然可以使用Double
但是Number
不可能,因为它不再满足T extends Comparable
( super
部分仍然可以工作)。
The next step is to understand that the expression between static
and List
just declares the properties of the type T
which is used later in the code. 下一步是要理解static
和List
之间的表达式只声明了类型T
的属性,后者在代码中使用。 This way, you don't have to repeat this long declaration over and over again. 这样,您就不必一遍又一遍地重复这个长的声明。 It's part of the behavior of the method (like public
) and not part of the actual code. 它是方法行为的一部分(如public
),而不是实际代码的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.