[英]Is there a type-safe way to pass an empty list as an argument in java?
以下代码给出了编译错误:
public void method(List<String> aList) {}
public void passEmptyList() {
method(Collections.emptyList());
}
有没有办法将空列表传递给method
而没有
new ArrayList<String>()
?
更换
method(Collections.emptyList());
同
method(Collections.<String>emptyList());
之后的<String>
.
是emptyList
的类型参数的显式绑定,因此它将返回List<String>
而不是List<Object>
。
您可以像这样指定类型参数:
public void passEmptyList() {
method(Collections.<String>emptyList());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.