Java Collections.emptyList() 返回一个 List<Object>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/306713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Collections.emptyList() returns a List<Object>?
提问by Chris Conway
I'm having some trouble navigating Java's rule for inferring generic type parameters. Consider the following class, which has an optional list parameter:
我在浏览 Java 推断泛型类型参数的规则时遇到了一些麻烦。考虑以下类,它有一个可选的列表参数:
import java.util.Collections;
import java.util.List;
public class Person {
private String name;
private List<String> nicknames;
public Person(String name) {
this(name,Collections.emptyList());
}
public Person(String name,List<String> nicknames) {
this.name = name;
this.nicknames = nicknames;
}
}
My Java compiler gives the following error:
我的 Java 编译器给出以下错误:
Person.java:9: The constructor Person(String, List<Object>) is undefined
But Collections.emptyList()
returns type <T> List<T>
, not List<Object>
. Adding a cast doesn't help
但Collections.emptyList()
返回 type <T> List<T>
,而不是List<Object>
。添加演员没有帮助
public Person(String name) {
this(name,(List<String>)Collections.emptyList());
}
yields
产量
Person.java:9: inconvertible types
Using EMPTY_LIST
instead of emptyList()
使用EMPTY_LIST
代替emptyList()
public Person(String name) {
this(name,Collections.EMPTY_LIST);
}
yields
产量
Person.java:9: warning: [unchecked] unchecked conversion
Whereas the following change makes the error go away:
而以下更改使错误消失:
public Person(String name) {
this.name = name;
this.nicknames = Collections.emptyList();
}
Can anyone explain what type-checking rule I'm running up against here, and the best way to work around it? In this example, the final code example is satisfactory, but with larger classes, I'd like to be able to write methods following this "optional parameter" pattern without duplicating code.
谁能解释一下我在这里遇到的类型检查规则,以及解决它的最佳方法?在这个例子中,最终的代码示例是令人满意的,但是对于更大的类,我希望能够按照这种“可选参数”模式编写方法而无需重复代码。
For extra credit: when is it appropriate to use EMPTY_LIST
as opposed to emptyList()
?
额外的信用:什么时候使用EMPTY_LIST
而不是emptyList()
?
采纳答案by InverseFalcon
The issue you're encountering is that even though the method emptyList()
returns List<T>
, you haven't provided it with the type, so it defaults to returning List<Object>
. You can supply the type parameter, and have your code behave as expected, like this:
您遇到的问题是,即使该方法emptyList()
返回了List<T>
,您也没有为其提供类型,因此它默认为返回List<Object>
。您可以提供类型参数,并让您的代码按预期运行,如下所示:
public Person(String name) {
this(name,Collections.<String>emptyList());
}
Now when you're doing straight assignment, the compiler can figure out the generic type parameters for you. It's called type inference. For example, if you did this:
现在,当您进行直接赋值时,编译器可以为您找出泛型类型参数。这称为类型推断。例如,如果你这样做:
public Person(String name) {
List<String> emptyList = Collections.emptyList();
this(name, emptyList);
}
then the emptyList()
call would correctly return a List<String>
.
那么emptyList()
调用将正确返回一个List<String>
.
回答by carson
You want to use:
你想使用:
Collections.<String>emptyList();
If you look at the source for what emptyList does you see that it actually just does a
如果您查看 emptyList 的来源,您会发现它实际上只是做了一个
return (List<T>)EMPTY_LIST;
回答by Dan Vinton
the emptyList method has this signature:
emptyList 方法具有以下签名:
public static final <T> List<T> emptyList()
That <T>
before the word List means that it infers the value of the generic parameter T from the type of variable the result is assigned to. So in this case:
这<T>
之前它推断通用参数T的从结果被分配给变量的类型的值的字列表的装置。所以在这种情况下:
List<String> stringList = Collections.emptyList();
The return value is then referenced explicitly by a variable of type List<String>
, so the compiler can figure it out. In this case:
然后,返回值由类型为 的变量显式引用List<String>
,因此编译器可以计算出来。在这种情况下:
setList(Collections.emptyList());
There's no explicit return variable for the compiler to use to figure out the generic type, so it defaults to Object
.
编译器没有显式返回变量用于确定泛型类型,因此默认为Object
.