Skip to content

Commit

Permalink
Merge pull request eugenp#10643 from mdhtr/mdhtr/BAEL-4179_primitive_…
Browse files Browse the repository at this point in the history
…to_object_array

BAEL-4179 Cast primitive type array into object array in java
  • Loading branch information
glmartin authored Apr 26, 2021
2 parents 00d1ccd + 8198abc commit ac73457
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core-java-modules/core-java-lang-oop-types-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Core Java Lang OOP - Types

This module contains articles about types in Java

### Relevant Articles:

-
22 changes: 22 additions & 0 deletions core-java-modules/core-java-lang-oop-types-2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>core-java-modules</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<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>
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<module>core-java-lang-oop-generics</module>
<module>core-java-lang-oop-modifiers</module>
<module>core-java-lang-oop-types</module>
<module>core-java-lang-oop-types-2</module>
<module>core-java-lang-oop-inheritance</module>
<module>core-java-lang-oop-methods</module>
<module>core-java-lang-oop-others</module>
Expand Down

0 comments on commit ac73457

Please sign in to comment.