I have this bit of ( counter-intuitive ) observations about generic wildcard notation used in collections.
The purpose of the wildcard notation List ext
Yes, your observation is correct. However, in the case you show, it's not very useful (you're electing to ignore useful information about the list). The assignment is more useful when it's implicit as you call a library function.
For example, say you had a library function that closed a collection of InputStreams. You could accept a List
, but that is unnecessarily restrictive. You could instead do this:
public void closeAll(Collection extends InputStream> streams) {
for ( InputStream stream : streams ) {
stream.close();
}
}
Now you can pass in a List
instead.