A cast to List<String>
of an Object actually is a cast to List
. At runtime String no longer is there. The IDE did warn about this. Later on when working with strings
items a runtime exception may be expected.
Another thing, List<Map>
misses generic parameters, and when used bare, like List list
, the compiler reverts to non-generic interpretation, dropping all warning of generic type violations.
For your use-case: a kind of simple "variable" map with differently typed variables is problematic. Provide your own run-time type info, and maybe revert to (actually unneeded) shoveling of data:
List<?> list = (List<?>) map.get("strings");
List<String> list2 = list.stream().map(String.class::cast).collect(Collectors.toList());