interface TypeConverter<T, E> {
T convert(E e);
}
class CollectionUtil() {
public static <E> List<T> convertToList(List<E> fromList, TypeConverter<T, E> conv) {
{
if(fromList== null) return null;
List<T> newList = new ArrayList<T>(fromList.size())
for(E e : fromList)
{
newList.add(conv.convert(e));
}
return newList;
}
}
Above code explains converting from List of String to List of Integer by implementing TypeConverter interface for String, Integer. Are there already any collections conversion utility methods exists in any API like list to set and so on?
In Guava:
Maven
In Collections 15 package:
Maven
In Functional Java
Maven
Note that this library uses different set of collections.
…bonus in Scala: