I have defined a method in Java like below
public void processData(Object input) {
if(!(input instanceof List || input instanceof Map)) {
throw new RuntimeException("Invalid argument type. method accept only Map type or List type");
}
if(input instanceof List) {
//Do something
} else if(input instanceof Map) {
//Do Something
}
}
The above method just works fine. But is there a way to use generics here to show compile time error if user tries calls the method with unexpected argument? Method that only accepts List or Map ? <T extends Collection> wont work because Map is not part of Collection. Is there any other way ?
Listas parameter, and one with aMapas parameter. Both can have the same name. (With a link if you want to read more about method overloading)