Java流 – 不能使用forEach修改列表的内容

huangapple go评论72阅读模式
英文:

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&lt;T, R&gt; 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&lt;Integer&gt; map(ArrayList&lt;Integer&gt; nums, Function&lt;Integer, Integer&gt; mapper) {
        ArrayList&lt;Integer&gt; result = new ArrayList&lt;&gt;(nums);
        result.forEach(e -&gt; e = mapper.apply(e));
        return result;
    }

    public static void main(String [] args){
        ArrayList&lt;Integer&gt; nums = IntStream.rangeClosed(1, 3).boxed().collect(Collectors.toCollection(ArrayList::new));
        ArrayList&lt;Integer&gt; squares = Mapper.map(nums, z -&gt; z * z);
        squares.forEach(System.out::println);
    }
}

答案1

得分: 3

这是因为在 result.forEach(e -&gt; ... ) 中,e 不是对列表元素的引用,而是列表中整数的一个值。可以将这行代码等价地表示为:

for (int e : nums) {
  e = e * e;
}

这样也不会更新 num 集合中的元素。

英文:

This is because in result.forEach(e -&gt; ... ) 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&lt;Integer&gt; map(ArrayList&lt;Integer&gt; nums, Function&lt;Integer, Integer&gt; mapper) {
    return nums.stream()
        .map(mapper)
        .collect(Collectors.toList());
}

答案3

得分: 0

这是因为在result.forEach(e -&gt; ... )中,"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 -&gt; ... ) 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&lt;Integer&gt; map(List&lt;Integer&gt; numbers, Function&lt;Integer, Integer&gt; mapper) {
        return numbers.stream()
            .map(mapper)
            .collect(Collectors.toList());
    }

    public static void main(String [] args){
        List&lt;Integer&gt; numbers = new ArrayList&lt;&gt;(Arrays. asList(1, 2));
        map(numbers, number -&gt; number * number).forEach(System.out::println);
    }
}

huangapple
  • 本文由 发表于 2020年4月10日 11:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61133426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定