Java: select from collection only elements of provided type
        Posted  
        
            by glaz666
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by glaz666
        
        
        
        Published on 2010-04-27T15:07:55Z
        Indexed on 
            2010/04/27
            15:23 UTC
        
        
        Read the original article
        Hit count: 266
        
java
|collections
Hi!
I have a collection of elements of type B and C, which all extends A. I need to filter the collection to get elements of only B type.
Is there any way to do it except:
for (A a : initCollection) {
  if (a instance of B) {
    newCollection.add(a)?
  }
}
Thanks
© Stack Overflow or respective owner