Add examples

This commit is contained in:
mdhtr 2021-04-09 11:32:02 +02:00
parent daea725097
commit d52775ff98
2 changed files with 88 additions and 0 deletions

View File

@ -12,4 +12,11 @@
<artifactId>core-java-lang-oop-types-2</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,81 @@
package com.baeldung.conversions;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
class PrimitiveToObjectArrayUnitTest {
@Test
void givenUsingIteration_whenConvertingToObjects_thenSuccess() {
int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };
Integer[] output = new Integer[input.length];
for (int i = 0; i < input.length; i++) {
output[i] = input[i];
}
assertArrayEquals(expected, output);
}
@Test
void givenUsingIteration_whenConvertingToPrimitives_thenSuccess() {
Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };
int[] output = new int[input.length];
for (int i = 0; i < input.length; i++) {
output[i] = input[i];
}
assertArrayEquals(expected, output);
}
@Test
void givenUsingStreams_whenConvertingToObjects_thenSuccess() {
int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };
Integer[] output = Arrays.stream(input)
.boxed()
.toArray(Integer[]::new);
assertArrayEquals(expected, output);
}
@Test
void givenUsingStreams_whenConvertingToPrimitives_thenSuccess() {
Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };
int[] output = Arrays.stream(input)
.mapToInt(Integer::intValue)
.toArray();
assertArrayEquals(expected, output);
}
@Test
void givenUsingApacheCommons_whenConvertingToObjects_thenSuccess() {
int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };
Integer[] output = ArrayUtils.toObject(input);
assertArrayEquals(expected, output);
}
@Test
void givenUsingApacheCommons_whenConvertingToPrimitives_thenSuccess() {
Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };
int[] output = ArrayUtils.toPrimitive(input);
assertArrayEquals(expected, output);
}
}