tricky static generic method with generic return type which itself could be a generic

Specify the type on your call, rather than letting java infer the type:

Integer elem = MyConverter.<ArrayList<Integer>>convert(ar, "java.util.ArrayList<Integer>");

This link describes this (cool) syntax.


This kind of looks like Arrays.asList, it will take a native array and convert it to an ArrayList.

An implementation could like the following:

 public static <T> List<T> asList(T... a) {
    ArrayList<T> arr = new ArrayList<T>();
    for (T item: a) {
        arr.add(item);
    }
    return arr;
}

Tags:

Java

Generics