Considering these elements :
An interface I :
public interface I {
int getType();
}
A class C :
public class C implements I {
@Override
public int getType() { return 0; }
}
Given a list Of I, how can I make an ArrayList of C using Java 8 ?
Here is what I tried :
public void foo(List<? extends I> listI) {
List<C> listC = new ArrayList<>((List<C>)listI.stream()
.filter(o -> o.getType() == 0)
.collect(Collectors.toList())
);
}
And the warning I got :
Unchecked cast:
'java.util.List<capture<? extends I>>'to'java.util.List<C>'