How do I convert from List<?> to List<T> in Java using generics?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In Java, how do I convert List<?> to List<T> using a general purpose method so that I can replace patterns like the following with a single method call: List untypedList = new ArrayList(); // or returned from a legacy method List<Integer> typedList = new ArrayList<Integer>(); for (Object item: untypedList) typedList.add((Integer)item); Note that the above code does not generate any type-safety warnings and, ideally, your solution shouldn't generate any such warnings, either. Will the following solution work provided that list Class<L> has a