英文:
Java stream - Cannot modify the contents of a list with forEach
问题
我必须在下面实现一个名为 `map()` 的方法,该方法接收一个数组列表和一个 `Function<T, R>` 对象,并返回一个由将该函数应用于给定元素的结果组成的数组列表。
以下代码打印出 `1,2,3` 而不是这些数字的平方。为什么代码不起作用?我需要失败的原因,而不是正确的解决方案。
import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Mapper {
public static ArrayList<Integer> map(ArrayList<Integer> nums, Function<Integer, Integer> mapper) {
ArrayList<Integer> result = new ArrayList<>(nums);
result.forEach(e -> e = mapper.apply(e));
return result;
}
public static void main(String [] args){
ArrayList<Integer> nums = IntStream.rangeClosed(1, 3).boxed().collect(Collectors.toCollection(ArrayList::new));
ArrayList<Integer> squares = Mapper.map(nums, z -> z * z);
squares.forEach(System.out::println);
}
}
英文:
I have to implement a method map()
below which receives an array list and a Function<T, R>
object, and
returns an array list consisting of the results of applying the function to the given elements.
The following code prints 1,2,3
instead of squares of those numbers. Why does the code not work? I need the reason for failure and not correct solutions.
import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Mapper {
public static ArrayList<Integer> map(ArrayList<Integer> nums, Function<Integer, Integer> mapper) {
ArrayList<Integer> result = new ArrayList<>(nums);
result.forEach(e -> e = mapper.apply(e));
return result;
}
public static void main(String [] args){
ArrayList<Integer> nums = IntStream.rangeClosed(1, 3).boxed().collect(Collectors.toCollection(ArrayList::new));
ArrayList<Integer> squares = Mapper.map(nums, z -> z * z);
squares.forEach(System.out::println);
}
}
答案1
得分: 3
这是因为在 result.forEach(e -> ... )
中,e
不是对列表元素的引用,而是列表中整数的一个值。可以将这行代码等价地表示为:
for (int e : nums) {
e = e * e;
}
这样也不会更新 num
集合中的元素。
英文:
This is because in result.forEach(e -> ... )
e
is not a reference to the list element, but a value of integer from the list. Think of that line as equivalent to this:
for (int e : nums) {
e = e * e;
}
This would not update elements in the num
collection either.
答案2
得分: 0
根据您的情况,使用 map
更为合适:
public static ArrayList<Integer> map(ArrayList<Integer> nums, Function<Integer, Integer> mapper) {
return nums.stream()
.map(mapper)
.collect(Collectors.toList());
}
英文:
In your case map
would be more appropriate:
public static ArrayList<Integer> map(ArrayList<Integer> nums, Function<Integer, Integer> mapper) {
return nums.stream()
.map(mapper)
.collect(Collectors.toList());
}
答案3
得分: 0
这是因为在result.forEach(e -> ... )
中,"e"不是一个对元素的引用,而是对值的引用。
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.*;
import java.util.stream.*;
public class Mapper {
public static List<Integer> map(List<Integer> numbers, Function<Integer, Integer> mapper) {
return numbers.stream()
.map(mapper)
.collect(Collectors.toList());
}
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2));
map(numbers, number -> number * number).forEach(System.out::println);
}
}
英文:
This is because in result.forEach(e -> ... )
where "e" is not a reference an element, instead it's a reference to the value.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.*;
import java.util.stream.*;
public class Mapper {
public static List<Integer> map(List<Integer> numbers, Function<Integer, Integer> mapper) {
return numbers.stream()
.map(mapper)
.collect(Collectors.toList());
}
public static void main(String [] args){
List<Integer> numbers = new ArrayList<>(Arrays. asList(1, 2));
map(numbers, number -> number * number).forEach(System.out::println);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论