Get generic type of java.util.List

后端 未结 14 2314
广开言路
广开言路 2020-11-22 02:06

I have;

List stringList = new ArrayList();
List integerList = new ArrayList();

Is

14条回答
  •  心在旅途
    2020-11-22 02:25

    As others have said, the only correct answer is no, the type has been erased.

    If the list has a non-zero number of elements, you could investigate the type of the first element ( using it's getClass method, for instance ). That won't tell you the generic type of the list, but it would be reasonable to assume that the generic type was some superclass of the types in the list.

    I wouldn't advocate the approach, but in a bind it might be useful.

提交回复
热议问题