0

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:

[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..

Is it possible to do it with the reduce?

  • 1
    Does this answer your question? [How can I concatenate two arrays in Java?](https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) – Eggcellentos May 12 '20 at 11:54
  • Why do you want to use reduce? The first and second option are way easier to understand – Lino May 12 '20 at 12:00
  • @ghosh I saw this post, kind of but not really, because there is one answer which is using reduce but inside the reduce he is using Arrays.copyOf and System.arraycopy which is not how i want to achieve the merge of the two arrays, i am looking for some other alternative if there is one. – unknownerror May 12 '20 at 12:10
  • @Line i really like the reduce function and because it is not very used and maybe readable in some cases (as you stated) i am exploring it's capabilities and different approaches of this simple problem.. – unknownerror May 12 '20 at 12:11

2 Answers2

0

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. 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.

0

This works. It is contrived. But then any way of doing this with reduce would, imho, be contrived since there are much better ways.

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

System.out.println(Arrays.toString(result));
WJS
  • 36,363
  • 4
  • 24
  • 39