Why does IntelliJ tell this casting from Object to String is redundant?
public class Utils {
private static Map<Class, Function<Object, ?>> functions = new HashMap<>();
static {
functions.put(String.class, v -> (String) v);
functions.put(Foo.class, v -> {
Foo foo = new Foo();
foo.setA((String) ((Map<String, Object>) v).get("A"));
foo.setB((String) ((Map<String, Object>) v).get("B"));
return foo;
});
}
}
The value type of the map is Function<Object, ?>
- i.e., it doesn't matter what the return type of the function is.
Since casting doesn't really "do" anything, just let's the compiler know the type of the object, and since you don't need it to be a string in order to compile, you can just remove it.
Having said that, there also is no reason to "reimplement" the function v -> v
, and you could use Function.identity()
instead.