I have two clases:
public class A {
//attibutes and methods
}
public class B extends A {
//atributes and methods
}
Now I have a service that returns a List with elements of type A. Lets call it generateAElements();
I want to call that method, filter the List obtained to only keep the elements of type B, which also are of type A.
List<A> listA = generateAElements();
List<A> listAA = listA.filter( p -> p instanceof B).collect(Collectors.toList());
List<B> listB = new ArrayList<>();
// need to create a new list, iterate overListA and add elements of type B?
for (A itemA : listA) {
listB.add((B) itemA);
}
Is there an efficient way to do this?
Important: The list may contain a large number of elements.