Create a copy of the given list with mapper applied on each item. Opposed to java.util.stream.Stream#map(Function) / Collectors#toList() this minimizes allocations.
(Collection<I> list, Function<? super I, ? extends O> mapper)
| 154 | * Opposed to {@link java.util.stream.Stream#map(Function)} / {@link Collectors#toList()} this minimizes allocations. |
| 155 | */ |
| 156 | public static <I, O> List<O> map(Collection<I> list, Function<? super I, ? extends O> mapper) { |
| 157 | if (list.isEmpty()) { |
| 158 | return List.of(); |
| 159 | } |
| 160 | ArrayList<O> copy = new ArrayList<>(list.size()); |
| 161 | for (I item : list) { |
| 162 | copy.add(mapper.apply(item)); |
| 163 | } |
| 164 | return copy; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Like `map` but ensures that the same list is returned if no elements changed. |