So I have the following class
public abstract class DeliveryFileType<T> {
public abstract Stream<T> getStream();
public abstract ArrayList<String> processRecordToArrayList(T t);
}
Then the code
DeliveryFileType<?> deliveryFileType = DeliveryFileTypeFactory.createDeliveryType(deliveryFileStore, delivery.getFileLocation());
Flowable<ArrayList<String>> recordStream = Flowable.fromStream(deliveryFileType.getStream()).map(record -> deliveryFileType.processRecordToArrayList(record));
This createDeliveryType
returns a DeliveryFileType<?>
, in which depending on the parameter it can return diffent types, e.g., DeliveryFileType<String>
, DeliveryFileType<Map<String, String>>
, etc.
With this code, Java complains java.lang.Object cannot be converted to capture of ?
referring to record
on deliveryFileType.processRecordToArrayList(record)
in the second line.
Why does this happen? The method getStream
return an stream of elements T
, so record should also be from the same type T
, not Object
.
If I change the code declaration in the first snippet to:
DeliveryFileType<Object> deliveryFileType = ...
and the method createDeliveryType
in the factory to return a DeliveryFileType
, instead of DeliveryFileType<?>
, the error goes away.
This question is similar, but it doesn't answer my quetion, because I'm not talking about subtypes here. Java generics, Unbound wildcards <?> vs <Object>