Suppose I have the following method, which can be used to create a collection of a given type specified.
private static Collection<?> void create(Class<? extends Collection<?>> cls) {
return cls.newInstance();
}
This is all good if the cls argument is passed in during runtime:
List<String> list = new LinkedList<String>();
create(list.getClass());
But how do I invoke this method in code without an unchecked warning? Say I want to do something like:
create(LinkedList.class);
It'll complain that create(Class) is not defined, which strictly speaking is correct because List is not #capture of Collection, but how do I make it work?
Many thanks!