Generic types is a compile time abstraction. At runtime all maps will have the same type Map<Object, Object>
. So if you are sure that values are strings, you can cheat on java compiler:
Map<String, Object> m1 = new HashMap<String, Object>();
Map<String, String> m2 = (Map) m1;
Copying keys and values from one collection to another is redundant. But this approach is still not good, because it violates generics type safety. May be you should reconsider your code to avoid such things.