Java tutorial
//package com.java2s; import java.util.List; import java.util.stream.Collectors; public class Main { public static <T> T firstOfType(List<Object> list, Class<T> targetClass) { for (Object object : list) { boolean isTargetClass = object.getClass().equals(targetClass); if (isTargetClass) { return targetClass.cast(object); } } return null; } @SuppressWarnings("unchecked") public static <TargetType, CurrentType> List<TargetType> cast(List<CurrentType> list, Class<TargetType> targetClass) { return list.stream().map(e -> (TargetType) e).collect(Collectors.toList()); } }