Here you can find the source of ofType(Stream
@SuppressWarnings("unchecked") public static <T, U> Stream<U> ofType(Stream<T> stream, Class<U> type)
//package com.java2s; //License from project: Open Source License import java.util.stream.Stream; public class Main { /**/*ww w.j a v a 2 s.c o m*/ * Keep only those elements in a stream that are of a given type. * * * assertThat(Arrays.asList(1, 2, 3), * equalTo( StreamUtils.ofType(Stream.of(1, "a", 2, "b", 3,Integer.class)); * */ @SuppressWarnings("unchecked") public static <T, U> Stream<U> ofType(Stream<T> stream, Class<U> type) { return stream.filter(type::isInstance).map(t -> (U) t); } }