user496934 :
I am trying to understand the usage of lower bound wildcards in some depth. I am trying to write a generic method copy
which copies the contents of one List
to another. I came up with this method signature:
<T> void copy(List<T> dest, List<? extends T> src)
I think this signature is comprehensive to address all scenarios. However, I see that in the Java Collections class the method signature is like this:
<T> void copy(List<? super T> dest, List<? extends T> src)
I do not understand why they use List<? super T> dest
instead of just List<T> dest
. Is there some extra flexibility with their signature?
Eran :
Here's an example:
The following snippet passes compilation with the signature <T> void copy(List<? super T> dest, List<? extends T> src)
but doesn't work with the signature <T> void copy(List<T> dest, List<? extends T> src)
:
YourClass obj = new YourClass ();
List<HashMap<String,String>> lhm = new ArrayList<>();
List<Map<String,String>> lm = new ArrayList<>();
obj.<HashMap<String,String>>copy (lm,lhm);
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=438898&siteId=1