forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#10643 from mdhtr/mdhtr/BAEL-4179_primitive_…
…to_object_array BAEL-4179 Cast primitive type array into object array in java
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
||
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
81 changes: 81 additions & 0 deletions
81
...ng-oop-types-2/src/test/java/com/baeldung/conversions/PrimitiveToObjectArrayUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters