Is there a way in Java 8 to transform an Array/Collection using map()
without having to reassign the created copy, e.g.
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(" ", ""));
Ideone demo
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With