Suppose I want to write a function, which will create a HashMap from some specified type T to String, for example, a HashMap from Integer to String as follows:
HashMap<Integer, String> myHashMay = new HashMap<Integer, String>()
;
I want to have flexibilty to specify the type T. So I write a function as:
void foo(Class<?> myClass) {
HashMap<myClass, String> myHashMay = new HashMap<myClass, String>();
.......
}
so that if I invoke foo(Integer.class), a HashMap from Integer to String will be created inside this function. Apparently the above foo function does not even compile. Could anybody give me some hints on how to write such a function correctly with the given function signature.
Thanks,