English 中文(简体)
在Java 8利用功能从一个阵列中提取一对一定数量的ger
原标题:Find a pair of integers from an array that sums up to a given number in Java 8 using functionals

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利用职能方案进行这项工作吗? 如果回答是,那么如何?

最佳回答

can/em>将其定稿。 否则,你就可以写字。

for (int i = 0; i < arr.length; i++) {
    final int[] arrFinal = arr;
    final int iFinal = i;
    List<Integer> res = IntStream.of(arr).boxed()
        .filter(x -> x + arrFinal[iFinal] == givenNumber)
        .collect(Collectors.toList());
}

页: 1

for (int i = 0; i < arr.length; i++) {
    int target = givenNumber - arr[i];
    List<Integer> res = IntStream.of(arr).filter(x -> x == target)
                           .boxed().collect(Collectors.toList());
}

......这还表明,就你的价值而言,你只是重复一些时候,可能首先重塑你的方案。

问题回答

这些编号为complementary pairs,而且完全可以与流体相同:

 private static void showPairs(int k, int[] array) {

    IntStream.range(0, array.length)
            .boxed()
            .flatMap(i -> IntStream.range(i, array.length)
                    .boxed()
                    .filter(j -> k - array[i] == array[j])
                    .flatMap(j -> Stream.of(new int[] { i, j }, new int[] { j, i }))) // [i, j] and [j, i]
            .forEach(arr -> System.out.println(Arrays.toString(arr)));
}
    Set<String> set = new LinkedHashSet<>();
    for (int i = 0; i < array.length - 1; i++) {
        final int pos = i;
        Arrays.stream(array).skip(pos + 1).filter(element -> array[pos] + element == sumTarget).forEach(element -> {
            set.add("{" + array[pos] + ", " + element + "}");
        });
    }
    set.forEach(System.out::println);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签