Guava was mentioned in other answers, but not a specific solution, which is even simpler than people understand:
Iterable<B> onlyBs = Iterables.filter(initCollection, B.class);
It is simple and clean, does the right thing, creates only one instance and does not copy anything and does not cause any warnings.
(The Collections2.filter() method does not have this particular overload, so if you really want Collection , you need to provide Predicates.instanceOf(B.class) , and the resulting collection will still be sadly of type Collection<A> .)
source share