简体   繁体   English

Java 如何将两个 arrays 与减少 function 合并为一个,如果可能的话?

[英]Java how to reduce/combine two arrays into one with the reduce function, if possible?

I have this code:我有这个代码:

String[] arr1 = {"asd1", "asd2", "asd3", "asd4", "asd5", "asd6", "asd7"};
    String[] arr2 = {"asd8", "asd9", "asd10", "asd11", "asd12", "asd13", "asd14"};

    String[] concatenated = Stream.of(arr1, arr2)
           .flatMap(Stream::of)
           .toArray(String[]::new);

    String[] concatenated = Stream
            .concat(Arrays.stream(arr1), Arrays.stream(arr2))
            .toArray(String[]::new);

    String[] concatenated = Stream.of(arr1, arr2)
            .reduce(new String[arr1.length + arr2.length],
                    (String[] a, String[] b) -> )); ///// how to proceed from here?

    System.out.println(Arrays.toString(concatenated));

I am playing around and i didn't managed to find how to use the reduce function to combine them both to get this result:我在玩,我没有设法找到如何使用减少 function 将它们结合起来得到这个结果:

[asd1, asd2, asd3, asd4, asd5, asd6, asd7, asd8, asd9, asd10, asd11, asd12, asd13, asd14] [asd1, asd2, asd3, asd4, asd5, asd6, asd7, asd8, asd9, asd10, asd11, asd12, asd13, asd14]

note: The order doesn't really matter, the important thing is to get them both into one array with the reduce function..注意:顺序并不重要,重要的是通过减少 function 将它们放入一个数组中。

Is it possible to do it with the reduce?可以用reduce做到吗?

Well for the moment this is the best i come up with, if someone is interested:好吧,目前这是我想出的最好的,如果有人感兴趣的话:

String[] concatenated = Stream
            .of(arr1, arr2)
            .reduce((a, b) -> {
                String[] result = Arrays.copyOf(a, a.length + b.length);
                System.arraycopy(b, 0, result, a.length, b.length);
                return result;
            })
            .orElseThrow();

    Stream.of(arr1, arr2)
            .reduce((a, b) -> {
                String[] result = Arrays.copyOf(a, a.length + b.length);
                System.arraycopy(b, 0, result, a.length, b.length);
                return result;
            })
            .ifPresent(strings -> System.out.println(Arrays.toString(strings)));

    Optional<String[]> concatenated2 = Stream
            .of(arr1, arr2)
            .reduce((a, b) -> {
                String[] result = Arrays.copyOf(a, a.length + b.length);
                System.arraycopy(b, 0, result, a.length, b.length);
                return result;
            });

    System.out.println(Arrays.toString(concatenated));

    concatenated2.ifPresent(strings -> System.out.println(Arrays.toString(strings)));

Not exactly what i am looking for.不完全是我要找的。 I would be glad to see if someone can come up with a cleaner solution using reduce to solve this problem.我很高兴看到是否有人可以使用 reduce 提出更清洁的解决方案来解决这个问题。 I know i can make it cleaner if i put the body of the reduce in a method reference or something, but that's not really the case.我知道如果我将 reduce 的主体放在方法引用或其他东西中,我可以让它更干净,但事实并非如此。

This works.这行得通。 It is contrived.这是人为的。 But then any way of doing this with reduce would, imho, be contrived since there are much better ways.但是,恕我直言,任何使用reduce的方法都是人为的,因为有更好的方法。

String[] result = Stream.of(arr1, arr2)
                .flatMap(Arrays::stream)
                .reduce("", (s1, s2) -> s1 + s2 + ",").split(",");

System.out.println(Arrays.toString(result));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM