java – An easy way to concatenate arrays of strings

Question:

Good day!

I need to concatenate two arrays of string type, in what simple way can I do this?

String[] bothArray(String[] first, String[] second) {
    return ??;
}

Answer:

If Java8 is allowed , then you can use the Stream class and lambdas:

import java.util.Arrays;
import java.util.stream.Stream;

// . . .

String[] bothArray(final String[] first, final String[] second)
{
    return Stream.concat(Stream.of(first), Stream.of(second)).toArray(String[]::new);
}

Compact, beautiful and requires no external components.

Scroll to Top