Reputation: 7375
I've a read only method that should be able to take either
1. Map<Date, List<X>>
or
2. Map<Date, List<Y>>
as a parameter.
Here, I've the following two options to define the method.
A. private <T> List<Date> myMethod(Map<Date, List<T>> map)
B. private List<Date> myMethod(Map<Date, List<?>> map)
Both work fine for me, which one is preferable?
Thanks.
Upvotes: 2
Views: 93
Reputation: 19185
From JLS
<T> boolean addAll(Collection<T> c)
This version is sufficiently flexible, but note that the type parameter is used only once in the signature. This reflects the fact that the type parameter is not being used to express any kind of interdependency between the type(s) of the argument(s), the return type and/or throws type. In the absence of such interdependency, generic methods are considered bad style, and wildcards are preferred.
Upvotes: 5
Reputation: 38265
The first one gives you access to the T
type in case you need that (for example, if you need to cast something to type T
or something like that). With the latter, you simply state that you don't give a damn what sort of elements that List
contains.
Upvotes: 5