I’m a newbie in Generic and my question is: what difference between two functions:
function 1:
public static <E> void funct1 (List<E> list1) {
}
function 2:
public static void funct2(List<?> list) {
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first signature says: list1 is a List of Es.
The second signature says: list is a List of instances of some type, but we don’t know the type.
The difference becomes obvious when we try to change the method so it takes a second argument, which should be added to the list inside the method:
The first one works nicely. And you can’t change the second argument into anything that will actually compile.
Actually I just found an even nicer demonstration of the difference:
One might as why do we need
<?>when it only restricts what we can do with it (as @Babu_Reddy_H did in the comments). I see the following benefits of the wildcard version:The caller has to know less about the object he passes in. For example if I have a Map of Lists:
Map<String, List<?>>I can pass its values to your function without specifying the type of the list elements. SoIf I hand out objects parameterized like this I actively limit what people know about these objects and what they can do with it (as long as they stay away from unsafe casting).
These two make sense when I combine them:
List<? extends T>. For example consider a methodList<T> merge(List<? extends T>, List<? extends T>), which merges the two input lists to a new result list. Sure you could introduce two more type parameters, but why would you want to? It would be over specifying things.addmethod work, whilegetdoesn’t give you anything useful. Of course that triggers the next question: why don’t generics have lower bounds?For a more in depth answer see: When to use generic methods and when to use wild-card? and http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ203