Java tutorial
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.util.List; import java.util.Set; public class Main { /** * Verifies that the passed list contains only elements of the given * type, and returns it as a parameterized type. Throws if any element * is a different type. */ @SuppressWarnings("unchecked") public static <T> List<T> cast(List<?> list, Class<T> klass) { for (Object obj : list) { klass.cast(obj); } return (List<T>) list; } /** * Verifies that the passed set contains only elements of the given * type, and returns it as a parameterized type. Throws if any element * is a different type. */ @SuppressWarnings("unchecked") public static <T> Set<T> cast(Set<?> set, Class<T> klass) { for (Object obj : set) { klass.cast(obj); } return (Set<T>) set; } }