parameter optionnal java code example
Example 1: java how to make a parameter optional
void foo(String a, Integer... b) {
Integer b1 = b.length > 0 ? b[0] : 0;
Integer b2 = b.length > 1 ? b[1] : 0;
}
foo("a");
foo("a", 1, 2);
Example 2: java how to make a parameter optional
void foo(Map<String, Object> parameters) {
String a = "";
Integer b = 0;
if (parameters.containsKey("a")) {
if (!(parameters.get("a") instanceof Integer)) {
throw new IllegalArgumentException("...");
}
a = (Integer)parameters.get("a");
}
if (parameters.containsKey("b")) {
}
}
foo(ImmutableMap.<String, Object>of(
"a", "a",
"b", 2,
"d", "value"));