Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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...)

example:

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

Or use Stream in Java 8:

String[] both = Stream.concat(Arrays.stream(a), Arrays.stream(b)) .toArray(String[]::new);

Or:

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)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...