0

I am trying to solve the problem of creating a List in runtime. I created method

private <GT, LT> LT getList(Class<GT> genericType, Class<LT> listLype) throws Exception {
    Constructor<LT> c = listLype.getConstructor();
    ...
    ???
}

where LT - type of List I want to create (ArrayList, LinkedList, etc), and GT - generic type of List. How can I createLT<GT> List?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sergey
  • 51
  • 2
  • Does this answer your question? [Create instance of generic type in Java?](https://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java) – pringi Feb 17 '22 at 15:51
  • 1
    `ArrayList` and `LinkedList` do not care about the element type. Use `return c.newInstance();` and you’re done; that’s compatible with your return type `LT`. You might notice that `GT` has no effect here at all. – Holger Feb 18 '22 at 15:16

0 Answers0