Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Created August 8, 2016 01:34
Show Gist options
  • Save A-pZ/15190eef32d5c35b557176338687a780 to your computer and use it in GitHub Desktop.
Save A-pZ/15190eef32d5c35b557176338687a780 to your computer and use it in GitHub Desktop.
ラムダ式と実質的final
package seren.example.questions;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
/**
* @author A-pZ
*/
public class StreamTest {
@Test
public void test() {
int value = 10;
List<Integer> arrays = Arrays.asList(1, 2, 3, 4, 5);
arrays.stream().forEach(element -> {
// これはコンパイルエラー。ラムダ式の中からローカル変数を変更してはいけない。
// このルールはローカルクラスからメソッドのローカル変数を利用するときの制限と同じ仕様で
// ローカル変数の扱いは不変でなければならない。
value = +1;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment