tags:

views:

236

answers:

1

Is it possible to make a method return a dynamic List type. Such as a method a(Object b) can return a List<Integer> when the b is Integer type?

+2  A: 

I note that you're asking about dynamic attributes. Don't forget that generics implement type erasure. That is, at runtime, a List is simply that. The generic hints are there simply for compile time.

So what you would be returning is a simple raw List. It doesn't have type information with it and a List<Integer> is identical to a List<Double>.

There's no reason why you can't implement an interface like:

List<T> getList(T param);

but that's a static definition and you have to implement particular types in your codebase.

Brian Agnew
I'm asking this question because there is a warning if I return raw List type. Thanks anyway.
Sefler