public static void someMethod(List < ? extends BaseDto > list) {
for (ChildDto dto : list) {
}
}
ChildDto extends BaseDto and here I'm sure its the list full of ChildDto.
I do know I can do something like this
for (TextApplicationDto dto : (List<TextApplicationDto>)list) {
but it does not look pretty.
Is there any better way of doing the casting?
I think the best way would be:
public static void someMethod(List < ? extends BaseDto > list) {
for (BaseDto dto : list) {
ChildDto taDTO = (ChildDto)dto;
// Whatever
}
}
It also allows you to use instanceof
to be sure that the list only contains ChildDto
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