Is there a way in Java 8 to transform an Array/Collection using map()
without having to reassign the created copy, eg
Arrays.stream(array).mapInPlace(x -> x / 100);
list.stream().mapInPlace(e -> e.replaceAll(" ", ""));
instead of
array = Arrays.stream(array).map(x -> x / 100).toArray();
list = list.stream().map(e -> e.replaceAll(" ", "")).collect(Collectors.toList());
?
If not, what's the reason for this design decision?
Maybe using List::replaceAll
in your case can help you :
Arrays.asList(array).replaceAll(x -> x / 100);
and
list.replaceAll(e -> e.replaceAll(" ", ""));
Good point from @Holger , in case you need the index you can use Arrays::setAll
:
Arrays.setAll(array, x -> array[x] / 100);
Or more better if you want your job to be in parallel you can use Arrays::parallelSetAll
:
Arrays.parallelSetAll(array, x -> array[x] / 100);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.