Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (2.6k points)

What is the easiest way to concatenate two arrays in JAVA?

void f(String[] first, String[] second)


    String[] both = ???


1 Answer

0 votes
by (46k points)

To concatenate two arrays in JAVA either use this one linear from old Apache Commons Lang Library:

ArrayUtils.addAll(T[], T...)


String[] both = ArrayUtils.addAll(first, second);

Or use Stream in Java 8:

String[] both = Stream.concat(, .toArray(String[]::new);


String[] both = Stream.of(a, b).flatMap(Stream::of) .toArray(String[]::new);

Related questions

+1 vote
2 answers
+2 votes
3 answers
0 votes
2 answers
0 votes
1 answer
asked Jul 10, 2019 in Java by Ritik (3.5k points)

Browse Categories