Often for this type of problem I find useful the following construct:
public final class Reinterpret {
@SuppressWarnings("unchecked")
public static <T> T cast(Object object) {
return (T)object;
}
}
Which you can use, e.g., like this:
...
public void process(Map<String, Object> data) {
// method can properly handle any Object value,
// including String
}
...
Map<String, String> stringData = ...
process(Reinterpret.<Map<String, Object>>cast(stringData));
...
As you can guess, that very precise weakening of Java generics type system was inspired by C++'s reinterpret_cast operator, and is applicable in similar contexts; however, it can also be used in a few others, such as when you want to reduce the verbosity of your casts, e.g.,
Map<String, Object> objectMap = Reinterpret.cast(stringMap);
Java 6's type inference is strong enough (or weak enough, depending on your perspective) to cast the right-hand-side expression to what the left-hand-side requires; notice, however, that this works fine for assignments, but NOT for function calls, which is why the example above requires fully specifying the parametric types in the call itself.
List<Map<String, String>>? Why did someone use generics, but then putObjectas the value type?