简体   繁体   中英

JAVA Dynamic List Type

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM