The following code gives a compile error:
public void method(List<String> aList) {}
public void passEmptyList() {
method(Collections.emptyList());
}
Is there a way to pass an empty list to method
without
new ArrayList<String>()
?
Replace
method(Collections.emptyList());
with
method(Collections.<String>emptyList());
The <String>
after the .
is an explicit binding for emptyList
's type parameter, so it will return a List<String>
instead of a List<Object>
.
You can specify the type param like so:
public void passEmptyList() {
method(Collections.<String>emptyList());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.