@ SuppressWarnings ( "unchecked" ) public static < T , U > Stream < U > ofType ( final Stream < T > stream , final Class < ? extends U > type ) { return stream . filter ( type :: isInstance ) . map ( t -> ( U ) t ) ; }
Keep only those elements in a stream that are of a given type .