Given the list of data types like ['long', 'int', 'short']
, how to select the best suitable data type that can handle any value of any of the given types?
In this case, best data type is long
. So return long
. I am already of a way(brute force) but I am looking for right way to tackle it. Suggest me the best suitable design.
Below is my current solution:
static Map<String, List<String>> map = new HashMap<>();
static {
map.add("long", Lists.newArrayList("long", "int", "short", "byte"));
map.add("int", Lists.newArrayList("int", "short", "byte"));
.
.
}
public void String suitableDataType(List<String> input) {
if (input.contains("long") && input.stream().allMatch(t -> map.get("long").contains(t)))
return "long";
else if(..int..)
return "int";
.
.
.
else
return "string";
}
Basically construct a map of data type to all possible data types it can support. And check manually in the order of long, int, short
within input
and also check if all the data types within input
are supported using map and return the respective type.
But I am not satisfied with this approach. So looking for best way to solve this.
Thanks.
Aucun commentaire:
Enregistrer un commentaire