[BAEL-4918] concat Arrays in java
This commit is contained in:
parent
cbb5548525
commit
36b21f7116
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.arrayconcat;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ArrayConcatUtil {
|
||||
private ArrayConcatUtil() {}
|
||||
|
||||
static <T> T[] concatWithCollection(T[] array1, T[] array2) {
|
||||
List<T> resultList = new ArrayList<>(array1.length + array2.length);
|
||||
Collections.addAll(resultList, array1);
|
||||
Collections.addAll(resultList, array2);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
//the type cast is safe as the array1 has the type T[]
|
||||
T[] resultArray = (T[]) Array.newInstance(array1.getClass().getComponentType(), 0);
|
||||
return resultList.toArray(resultArray);
|
||||
}
|
||||
|
||||
static <T> T[] concatWithArrayCopy(T[] array1, T[] array2) {
|
||||
T[] result = Arrays.copyOf(array1, array1.length + array2.length);
|
||||
System.arraycopy(array2, 0, result, array1.length, array2.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
static <T> T concatWithCopy2(T array1, T array2) {
|
||||
if (!array1.getClass().isArray() || !array2.getClass().isArray()) {
|
||||
throw new IllegalArgumentException("Only arrays are accepted.");
|
||||
}
|
||||
|
||||
Class<?> compType1 = array1.getClass().getComponentType();
|
||||
Class<?> compType2 = array2.getClass().getComponentType();
|
||||
|
||||
if (!compType1.equals(compType2)) {
|
||||
throw new IllegalArgumentException("Two arrays have different types.");
|
||||
}
|
||||
|
||||
int len1 = Array.getLength(array1);
|
||||
int len2 = Array.getLength(array2);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
//the cast is safe due to the previous checks
|
||||
T result = (T) Array.newInstance(compType1, len1 + len2);
|
||||
|
||||
System.arraycopy(array1, 0, result, 0, len1);
|
||||
System.arraycopy(array2, 0, result, len1, len2);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T[] concatWithStream(T[] array1, T[] array2) {
|
||||
return Stream.concat(Arrays.stream(array1), Arrays.stream(array2))
|
||||
.toArray(size -> (T[]) Array.newInstance(array1.getClass().getComponentType(), size));
|
||||
}
|
||||
|
||||
static int[] concatIntArraysWithIntStream(int[] array1, int[] array2) {
|
||||
return IntStream.concat(Arrays.stream(array1), Arrays.stream(array2)).toArray();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package com.baeldung.arrayconcat;
|
||||
|
||||
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
public class ArrayConcatUtilUnitTest {
|
||||
private final String[] strArray1 = { "element 1", "element 2", "element 3" };
|
||||
private final String[] strArray2 = { "element 4", "element 5" };
|
||||
private final String[] expectedStringArray = { "element 1", "element 2", "element 3", "element 4", "element 5" };
|
||||
|
||||
private final int[] intArray1 = { 0, 1, 2, 3 };
|
||||
private final int[] intArray2 = { 4, 5, 6, 7 };
|
||||
private final int[] expectedIntArray = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
@Test
|
||||
public void givenTwoStringArrays_whenConcatWithList_thenGetExpectedResult() {
|
||||
String[] result = ArrayConcatUtil.concatWithCollection(strArray1, strArray2);
|
||||
assertThat(result).isEqualTo(expectedStringArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoStringArrays_whenConcatWithCopy_thenGetExpectedResult() {
|
||||
String[] result = ArrayConcatUtil.concatWithArrayCopy(strArray1, strArray2);
|
||||
assertThat(result).isEqualTo(expectedStringArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoStrings_whenConcatWithCopy2_thenGetException() {
|
||||
String exMsg = "Only arrays are accepted.";
|
||||
try {
|
||||
ArrayConcatUtil.concatWithCopy2("String Nr. 1", "String Nr. 2");
|
||||
fail(String.format("IllegalArgumentException with message:'%s' should be thrown. But it didn't", exMsg));
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e).hasMessage(exMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArraysInDifferentTypes_whenConcatWithCopy2_thenGetException() {
|
||||
String[] strArray = new String[] { "test 1", "test 2" };
|
||||
Integer[] integerArray = new Integer[] { 7, 11 };
|
||||
String exMsg = "Two arrays have different types.";
|
||||
|
||||
try {
|
||||
ArrayConcatUtil.concatWithCopy2(strArray, integerArray);
|
||||
fail(String.format("IllegalArgumentException with message:'%s' should be thrown. But it didn't", exMsg));
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e).hasMessage(exMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenConcatWithCopy2_thenGetExpectedResult() {
|
||||
String[] result = ArrayConcatUtil.concatWithCopy2(strArray1, strArray2);
|
||||
assertThat(result).isEqualTo(expectedStringArray);
|
||||
|
||||
int[] intResult = ArrayConcatUtil.concatWithCopy2(intArray1, intArray2);
|
||||
assertThat(intResult).isEqualTo(expectedIntArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoStringArrays_whenConcatWithStream_thenGetExpectedResult() {
|
||||
String[] result = ArrayConcatUtil.concatWithStream(strArray1, strArray2);
|
||||
assertThat(result).isEqualTo(expectedStringArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoIntArrays_whenConcatWithIntStream_thenGetExpectedResult() {
|
||||
int[] intResult = ArrayConcatUtil.concatIntArraysWithIntStream(intArray1, intArray2);
|
||||
assertThat(intResult).isEqualTo(expectedIntArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenConcatWithCommonsLang_thenGetExpectedResult() {
|
||||
String[] result = ArrayUtils.addAll(strArray1, strArray2);
|
||||
assertThat(result).isEqualTo(expectedStringArray);
|
||||
|
||||
int[] intResult = ArrayUtils.addAll(intArray1, intArray2);
|
||||
assertThat(intResult).isEqualTo(expectedIntArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoStringArrays_whenConcatWithGuava_thenGetExpectedResult() {
|
||||
String[] result = ObjectArrays.concat(strArray1, strArray2, String.class);
|
||||
assertThat(result).isEqualTo(expectedStringArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoIntArrays_whenConcatWithGuava_thenGetExpectedResult() {
|
||||
int[] intResult = Ints.concat(intArray1, intArray2);
|
||||
assertThat(intResult).isEqualTo(expectedIntArray);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue