Java tutorial
//package com.java2s; import java.util.LinkedList; import java.util.List; public class Main { public static <T> List<T> filterByType(Iterable<?> collection, Class<T> expectedType) { List<T> filtered = new LinkedList<T>(); if (collection != null) { for (Object item : collection) { if (item != null && expectedType.isInstance(item)) { filtered.add(expectedType.cast(item)); } } } return filtered; } }