Modifying local variable from inside lambda

后端 未结 11 1207
失恋的感觉
失恋的感觉 2020-11-28 04:30

Modifying a local variable in forEach gives a compile error:

Normal

    int ordinal = 0;
    for (Example s : list) {
          


        
相关标签:
11条回答
  • 2020-11-28 04:43

    Use a wrapper

    Any kind of wrapper is good.

    With Java 8+, use either an AtomicInteger:

    AtomicInteger ordinal = new AtomicInteger(0);
    list.forEach(s -> {
      s.setOrdinal(ordinal.getAndIncrement());
    });
    

    ... or an array:

    int[] ordinal = { 0 };
    list.forEach(s -> {
      s.setOrdinal(ordinal[0]++);
    });
    

    With Java 10+:

    var wrapper = new Object(){ int ordinal = 0; };
    list.forEach(s -> {
      s.setOrdinal(wrapper.ordinal++);
    });
    

    Note: be very careful if you use a parallel stream. You might not end up with the expected result. Other solutions like Stuart's might be more adapted for those cases.

    For types other than int

    Of course, this is still valid for types other than int. You only need to change the wrapping type to an AtomicReference or an array of that type. For instance, if you use a String, just do the following:

    AtomicReference<String> value = new AtomicReference<>();
    list.forEach(s -> {
      value.set("blah");
    });
    

    Use an array:

    String[] value = { null };
    list.forEach(s-> {
      value[0] = "blah";
    });
    

    Or with Java 10+:

    var wrapper = new Object(){ String value; }
    list.forEach(s->{
      wrapper.value = "blah";
    });
    
    0 讨论(0)
  • 2020-11-28 04:44

    If you only need to pass the value from the outside into the lambda, and not get it out, you can do it with a regular anonymous class instead of a lambda:

    list.forEach(new Consumer<Example>() {
        int ordinal = 0;
        public void accept(Example s) {
            s.setOrdinal(ordinal);
            ordinal++;
        }
    });
    
    0 讨论(0)
  • 2020-11-28 04:45

    As the used variables from outside the lamda have to be (implicitly) final, you have to use something like AtomicInteger or write your own data structure.

    See https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#accessing-local-variables.

    0 讨论(0)
  • 2020-11-28 04:45

    You can wrap it up to workaround the compiler but please remember that side effects in lambdas are discouraged.

    To quote the javadoc

    Side-effects in behavioral parameters to stream operations are, in general, discouraged, as they can often lead to unwitting violations of the statelessness requirement A small number of stream operations, such as forEach() and peek(), can operate only via side-effects; these should be used with care

    0 讨论(0)
  • 2020-11-28 04:53

    To have a more general solution, you can write a generic Wrapper class:

    public static class Wrapper<T> {
        public T obj;
        public Wrapper(T obj) { this.obj = obj; }
    }
    ...
    Wrapper<Integer> w = new Wrapper<>(0);
    this.forEach(s -> {
        s.setOrdinal(w.obj);
        w.obj++;
    });
    

    (this is a variant of the solution given by Almir Campos).

    In the specific case this is not a good solution, as Integer is worse than int for your purpose, anyway this solution is more general I think.

    0 讨论(0)
  • 2020-11-28 04:55

    If you are on Java 10, you can use var for that:

    var ordinal = new Object() { int value; };
    list.forEach(s -> {
        s.setOrdinal(ordinal.value);
        ordinal.value++;
    });
    
    0 讨论(0)
提交回复
热议问题