I wonder if it is possible to write a method that will handle casting of undefined types like:
Precondition:
Map<String, Object> map = new HashMap<>();
List<String> l = List.of("value");
map.put("key", l);
Method call:
List<String> strings = convert("key", List.class, String.class);
convert method itself:
public <V, C extends Collection<V>> C<V> getCollection(String key, Class<C> collType, Class<V> objectType) {
return (C<V>) map.get(key);
}
But statement C< V > doesn't compile and gives me an error: Type 'C' doesn't have type parameters.
Any ideas?
Java uses target typing in type inference which means the expected type on the right-hand side of an assignment can be leveraged for this purpose.
Map<String, Object> map = Map.of("strings", List.of("value"));
However, to take advantage of it an unchecked cast is needed:
<T> T convert(Map<String, Object> map, String key) {
return (T) map.get(key); // Warning: unchecked cast
}
You could then get the typed entry out of the map:
List<String> strings = convert(map, "strings");
However, because of type erasure of Java generics, the type parameter is not checked at runtime. So while this will throw a ClassCastException:
Set<String> setOfString = convert(map, "strings");
This will execute without any complaint, and you won't get a ClassCastException until you try to extract the values from the list:
List<Integer> numbers = convert(map, "strings");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With