/** * Map a set: generate a new set with each element mapped. The new set is always a {@link * HashSet}; it would have been more precise to use {@link java.lang.reflect reflection} to create * a set of the same type as 'srcSet', but reflection works really slowly in some implementations, * so it's best to avoid it. * * @throws IllegalArgumentException if srcSet == null */ public static <T, U> Set<U> mapToSet(Collection<T> srcSet, Function<T, U> f) throws IllegalArgumentException { if (srcSet == null) { throw new IllegalArgumentException("srcSet == null"); } HashSet<U> result = HashSetFactory.make(); for (Iterator<T> srcIter = srcSet.iterator(); srcIter.hasNext(); ) { result.add(f.apply(srcIter.next())); } return result; }
/** * Map a list: generate a new list with each element mapped. The new list is always an {@link * ArrayList}; it would have been more precise to use {@link java.lang.reflect reflection} to * create a list of the same type as 'srcList', but reflection works really slowly in some * implementations, so it's best to avoid it. * * @throws IllegalArgumentException if srcList == null */ public static <T, U> List<U> map(List<T> srcList, Function<T, U> f) throws IllegalArgumentException { if (srcList == null) { throw new IllegalArgumentException("srcList == null"); } ArrayList<U> result = new ArrayList<U>(); for (Iterator<T> srcIter = srcList.iterator(); srcIter.hasNext(); ) { result.add(f.apply(srcIter.next())); } return result; }