Join two arrays

Similar to joining two arraylists or collections, this example we will demonstrate how to combine the contents of a primitive array and an object array while using java, guava and apache commons. As shown in the setup, we initialize two string arrays that represent the state capitals and two primitive arrays of type int of an arbitrary index. Each example will return an array that will contain the concatenated contents while using a simple junit test case to validate the length of the array.

Setup

String[] firstHalfStateCapital = {
        "Montgomery",
        "Juneau",
        "Phoenix",
        ...
        "Jackson",
        "Jefferson City" };

String[] secondHalfStateCapital = {
        "Helena",
        "Lincoln",
        ...
        "Madison",
        "Cheyenne" };

int[] firstHalfStateCapitalByIndex = {
        1,
        2,
        3,
        ...
        23,
        24,
        25
};

int[] secondHalfStateCapitalByIndex = {
        26,
        27,
        28,
        ...
        48,
        49,
        50
};

Straight up Java

@Test
public void join_two_object_arrays_in_java () {

    String[] allStateCapitals = new String[firstHalfStateCapital.length + secondHalfStateCapital.length];

    // copy first half
    System.arraycopy(firstHalfStateCapital, 0, allStateCapitals, 0, firstHalfStateCapital.length);

    // copy second half
    System.arraycopy(secondHalfStateCapital, 0, allStateCapitals, firstHalfStateCapital.length, secondHalfStateCapital.length);

    assertTrue(allStateCapitals.length == 50);
}

Java 8

By first converting the arrays to a stream and using java 8 stream we can concatenate two object arrays using java 8.

Join object arrays

@Test
public void join_two_object_arrays_in_java8() {

    Stream<String> sa = Arrays.stream(firstHalfStateCapital);
    Stream<String> sb = Arrays.stream(secondHalfStateCapital);

    String[] allStateCapitals = Stream.concat(sa, sb).toArray(
            size -> new String[size]);

    assertTrue(allStateCapitals.length == 50);
}

Google Guava

Two guava utility classes, ObjectArrays handles object arrays and Ints pertain to int primitives, contain series of static methods for dealing with respective types.

Join object arrays

@Test
public void join_two_object_arrays_in_java_with_guava () {

    String[] allStateCapitals = ObjectArrays.concat(firstHalfStateCapital, secondHalfStateCapital, String.class);

    assertTrue(allStateCapitals.length == 50);
}

Join primitive arrays

@Test
public void join_two_primitive_arrays_in_java_with_guava () {

    int[] allStateCapitalsByIndex = Ints.concat(firstHalfStateCapitalByIndex, secondHalfStateCapitalByIndex);

    Arrays.toString(allStateCapitalsByIndex);
    assertEquals(50, allStateCapitalsByIndex.length);
}

Apache Commons

Apache commons contains a class ArrayUtils that contain operations on arrays, primitive arrays and primitive wrapper arrays.

Join object arrays

@Test
public void join_two_object_arrays_in_java_with_apache_commons () {

    String[] allStateCapitals = ArrayUtils.addAll(firstHalfStateCapital, secondHalfStateCapital);

    assertEquals(50, allStateCapitals.length);
}

Join primitive arrays

@Test
public void join_two_primitive_arrays_in_java_with_apache_commons () {

    int[] allStateCapitalsByIndex = ArrayUtils.addAll(firstHalfStateCapitalByIndex, secondHalfStateCapitalByIndex);

    assertEquals(50, allStateCapitalsByIndex.length);

}