【问题标题】:Java how to reduce/combine two arrays into one with the reduce function, if possible?如果可能的话,Java如何使用reduce函数将两个数组减少/合并为一个?
【发布时间】:2020-08-28 05:58:09
【问题描述】:

我有这个代码:

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));

我在玩,但我没有找到如何使用 reduce 函数将它们结合起来得到这个结果:

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

注意:顺序并不重要,重要的是使用reduce函数将它们放入一个数组中。..

是否可以通过reduce来做到这一点?

【问题讨论】:

  • 这能回答你的问题吗? How can I concatenate two arrays in Java?
  • 为什么要使用reduce?第一个和第二个选项更容易理解
  • @ghosh 我看到了这篇文章,有点但不是真的,因为有一个答案是使用reduce,但在reduce 内部他使用的是Arrays.copyOf 和System.arraycopy,这不是我想要的为了实现两个数组的合并,如果有的话,我正在寻找其他替代方案。
  • @Line 我真的很喜欢 reduce 函数,因为它不是很常用而且在某些情况下可能可读(如你所说),我正在探索它的功能和解决这个简单问题的不同方法..跨度>

标签: java arrays functional-programming reduce


【解决方案1】:

目前这是我想出的最好的,如果有人感兴趣的话:

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)));

不完全是我想要的。我很高兴看到是否有人可以使用 reduce 提出更清洁的解决方案来解决此问题。我知道如果我将 reduce 的主体放在方法引用或其他东西中,我可以让它更干净,但事实并非如此。

【讨论】:

    【解决方案2】:

    这行得通。这是人为的。但是,恕我直言,任何使用 reduce 执行此操作的方法都是人为的,因为有更好的方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      相关资源
      最近更新 更多