I have a list of objects:
List<SomeType> myList;
I want to get a list of sub-types available in this list:
List<SomeChildType> myChildList = myList.stream().filter(e -> e instanceof SomeChildType).collect(??????)
I don't know how to collect to obtain the correct list type.
You need to cast the objects:
List<SomeChildType> myChildList = myList.stream()
.filter(SomeChildType.class::isInstance)
.map(SomeChildType.class::cast)
.collect(toList())
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