VOOZH about

URL: https://mkyong.com/java/java-how-to-join-arrays/

⇱ Java - How to join Arrays - Mkyong.com


Skip to content

In this article, we will show you a few ways to join a Java Array.

  1. Apache Commons Lang – ArrayUtils
  2. Java API
  3. Java 8 Stream

1. Apache Commons Lang – ArrayUtils

The simplest way is add the Apache Commons Lang library, and use ArrayUtils. addAll to join arrays. This method supports both primitive and object type arrays.

JoinArray.java

package com.mkyong.example.array;

import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;

public class JoinArray {

 public static void main(String[] args) {

 String[] s1 = new String[]{"a", "b", "c"};
 String[] s2 = new String[]{"d", "e", "f"};

 String[] result = ArrayUtils.addAll(s1, s2);

 System.out.println(Arrays.toString(result));

		int [] int1 = new int[]{1,2,3};
 int[] int2 = new int[]{4,5,6};

 int[] result2 = ArrayUtils.addAll(int1, int2);

 System.out.println(Arrays.toString(result2));

 }

}

Output

[a, b, c, d, e, f]
[1, 2, 3, 4, 5, 6]

For Maven user.

pom.xml

<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
 <version>3.4</version>
</dependency>

2. Java API

Pure Java API example, supports both primitive and generic types.

JoinArray.java

package com.mkyong.example.array;

import java.lang.reflect.Array;
import java.util.Arrays;

public class JoinArray {

 public static void main(String[] args) {

 String[] s1 = new String[]{"a", "b", "c"};
 String[] s2 = new String[]{"d", "e", "f"};
 String[] s3 = new String[]{"g", "h", "i"};

 String[] result = joinArrayGeneric(s1, s2, s3);

 System.out.println(Arrays.toString(result));

 int[] int1 = new int[]{1, 2, 3};
 int[] int2 = new int[]{4, 5, 6};
 int[] int3 = new int[]{7, 8, 9};

 int[] result2 = joinArray(int1, int2, int3);

 System.out.println(Arrays.toString(result2));

 }

 static <T> T[] joinArrayGeneric(T[]... arrays) {
 int length = 0;
 for (T[] array : arrays) {
 length += array.length;
 }

 //T[] result = new T[length];
 final T[] result = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), length);

 int offset = 0;
 for (T[] array : arrays) {
 System.arraycopy(array, 0, result, offset, array.length);
 offset += array.length;
 }

 return result;
 }

 static int[] joinArray(int[]... arrays) {
 int length = 0;
 for (int[] array : arrays) {
 length += array.length;
 }

 final int[] result = new int[length];

 int offset = 0;
 for (int[] array : arrays) {
 System.arraycopy(array, 0, result, offset, array.length);
 offset += array.length;
 }

 return result;
 }

	//create other overloaded primitive type - long, double...
	//static long[] joinArray(long[]... arrays) 
}

Output

[a, b, c, d, e, f, g, h, i]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

3. Java 8 Stream

Java 8 Stream example to join arrays.

JoinArray.java

package com.mkyong.example.array;

import java.util.Arrays;

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class JoinArray {

 public static void main(String[] args) {

 String[] s1 = new String[]{"a", "b", "c"};
 String[] s2 = new String[]{"d", "e", "f"};
 String[] s3 = new String[]{"g", "h", "i"};

		//join object type array
 String[] result = Stream.of(s1, s2, s3).flatMap(Stream::of).toArray(String[]::new);
 System.out.println(Arrays.toString(result));

 int [] int1 = new int[]{1,2,3};
 int[] int2 = new int[]{4,5,6};
 int[] int3 = new int[]{7,8,9};

		//join 2 primitive type array
 int[] result2 = IntStream.concat(Arrays.stream(int1), Arrays.stream(int2)).toArray();

		//join 3 primitive type array, any better idea?
 int[] result3 = IntStream.concat(Arrays.stream(int1), 
			IntStream.concat(Arrays.stream(int2), Arrays.stream(int3))).toArray();

 System.out.println(Arrays.toString(result2));

 System.out.println(Arrays.toString(result3));

 }

}

Output

[a, b, c, d, e, f, g, h, i]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Any better Java 8 Stream example? Do comment below.

References

  1. Apache Commons Lang – ArrayUtils
  2. Java 8 Stream JavaDoc
  3. Java 8: Replace traditional for loops with IntStreams
  4. Java 8 Stream Tutorial

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Guest
8 years ago

Hi, I’m following your Java 8 Tutorial to refresh my Java skills.

To concat primitive type arrays one could also write:

int[] result3 = Stream.of(int1, int2, int3).flatMapToInt(IntStream::of).toArray();

2
Reply
mkyong
7 years ago
Reply to  Guest

Thanks for your idea πŸ™‚

0
Reply
zgd
5 years ago

yes,prefect

0
Reply
guest
6 years ago

We can also use Guava’s Ints/Longs… to concat primitive arrays:

int[] first = {1, 2, 3};
int[] second = {4, 5, 6};
int[] third = {7, 8};
int[] concat = Ints.concat(first, second, third);

0
Reply
reza
8 years ago

very very good

0
Reply
anurag
8 years ago

this is a great post about how to join array
thanks

0
Reply
della pramukti raharjo
10 years ago

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class JoinArray {

public static void main(String[] args) {

String[] s1 = new String[] { β€œa”, β€œb”, β€œc” };

String[] s2 = new String[] { β€œd”, β€œe”, β€œf” };

List listString = new ArrayList();

for (String s : s1)

listString.add(s);

for (String s : s2)

listString.add(s);

String[] s3 = listString.toArray(new String[0]);

System.out.println(β€œValue List String :” + listString.toString());

System.out.println(β€œValue join array :” + Arrays.toString(s3));

}

}

0
Reply
j
9 years ago

Doing it this way, you might as well just do the following:

listString = new ArrayList(Arrays.asList(s1));
listString.addAll(Arrays.asList(s2));

1
Reply