Short answer: no.
What do you intend to do with the t
parameter within the method? Since Map
and Collection
have only Object
as their common supertype, the only methods you can call on t
will be those on Object
. (Even methods on both interfaces, such as size()
, will be rejected by the compiler.)
With that in mind, is there any reason you can't overload the method in this case? That is, define one implementation for each desired parameter type:
public void test(Map<?,?> t) { ... }
public void test(Collection<?> t) { ... }
If you don't want to do that for whatever reason, then it seems that you could equally just declare the method to take Object
and perform a run-time type check of the class. This is semantically equivalent since you'd have to cast to call Map
or Collection
-specific methods anyway, though it does mean that the type check is done at compile time instead of runtime. Java's type system doesn't let you express this union-type dependency though, so I don't see any other alternative.