I was looking at the problem of finding a pair of two integers from an array that sums up to a given number in Java in a different way. I wanted to use Java 8 functionals. I tried something like this:
for (int i = 0; i < arr.length; i++) {
List<Integer> res = IntStream.of(arr).boxed().filter(x -> x + arr[i] == givenNumber)
.collect(Collectors.toList());
}
but it won t work(error) because "arr[i]" is not final and can t be final in my approach. Can something like: filter(x -> y -> x + y == givenNumber) with x and y from arr work somehow?
在 Java8利用职能方案进行这项工作吗? 如果回答是,那么如何?