Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query regarding generics in Java

Tags:

java

generics

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.

like image 729
Manish Mulani Avatar asked Feb 13 '26 23:02

Manish Mulani


1 Answers

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.

like image 178
Costi Ciudatu Avatar answered Feb 15 '26 11:02

Costi Ciudatu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!