I have a method that should only accept a Map
whose key is of type String
and value of type Integer
or String
, but not, say, Boolean
.
For example,
map.put("prop1", 1); // allowed
map.put("prop2", "value"); // allowed
map.put("prop3", true); // compile time error
It is not possible to declare a Map as below (to enforce compile time check).
void setProperties(Map<String, ? extends Integer || String> properties)
What is the best alternative other than declaring the value type as an unbounded wildcard
and validating for Integer
or String
at runtime?
void setProperties(Map<String, ?> properties)
This method accepts a set of properties to configure an underlying service entity. The entity supports property values of type String
and Integer
alone. For example, a property maxLength=2
is valid, defaultTimezone=UTC
is also valid, but allowDuplicate=false
is invalid.