I have two constructors setup like so:
public XMessage(Information info, List<Object> results) {
this.information = info;
this.results = results;
}
public XMessage(Information info, Object result) {
this(info, Collections.singletonList(result));
}
I create the XMessage object by passing in an Information object and an ArrayList object. When I inspect the result, it is a singleton list wrapping the ArrayList item. Why doesn't Java use the more appropriate constructor and what are my options to force it?
Calls first constructor:
new XMessage(information, new ArrayList<Object>());
Calls second constructor:
new XMessage(information, new ArrayList<String>());
ArrayList<String>() is not treated as List<Object> while ArrayList<Object> is. Consider using the following constructor:
public XMessage(Information info, List<? extends Object> results)
as @LuiggiMendoza suggested below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With