This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed for the flexible style. Benchmarking has shown this to outperform `stream()`. @param collection the collection to map @param mapper the mapper function @param for two
(Collection<? extends T> collection, Function<? super T, ? extends R> mapper)
| 51 | * @return a map immutable list of results |
| 52 | */ |
| 53 | public static <T, R> ImmutableList<R> map(Collection<? extends T> collection, Function<? super T, ? extends R> mapper) { |
| 54 | assertNotNull(collection); |
| 55 | assertNotNull(mapper); |
| 56 | ImmutableList.Builder<R> builder = ImmutableList.builderWithExpectedSize(collection.size()); |
| 57 | for (T t : collection) { |
| 58 | R r = mapper.apply(t); |
| 59 | builder.add(r); |
| 60 | } |
| 61 | return builder.build(); |
| 62 | } |
| 63 | |
| 64 | public static <T, R> ImmutableSet<R> mapToSet(Collection<? extends T> collection, Function<? super T, ? extends R> mapper) { |
| 65 | assertNotNull(collection); |