Last active
December 18, 2015 11:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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