Skip to content

Instantly share code, notes, and snippets.

@lsantos
Last active December 18, 2015 11:19
Show Gist options
  • Save lsantos/5774809 to your computer and use it in GitHub Desktop.
Save lsantos/5774809 to your computer and use it in GitHub Desktop.
This class gets ride of those annoying eclipse warnings for type safety when you have an api like hibernate query, which wasn't prepared to work with Java generics.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.RandomAccess;
public class CollectionUtils {
public static <E> List<E> typeSafeCopy(@SuppressWarnings("rawtypes") List list, Class<E> clazz) {
List<E> returnList = (list instanceof RandomAccess) ? new ArrayList<E>(list.size()) : new LinkedList<E>();
@SuppressWarnings("rawtypes")
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
Object o = iterator.next();
returnList.add(clazz.cast(o));
}
return returnList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment