From daea7250978b9cbdfd271ff6e4ea0f10f0b4ee90 Mon Sep 17 00:00:00 2001 From: mdhtr Date: Fri, 9 Apr 2021 11:31:49 +0200 Subject: [PATCH 1/2] Create new module --- .../core-java-lang-oop-types-2/README.md | 7 +++++++ .../core-java-lang-oop-types-2/pom.xml | 15 +++++++++++++++ core-java-modules/pom.xml | 1 + 3 files changed, 23 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-types-2/README.md create mode 100644 core-java-modules/core-java-lang-oop-types-2/pom.xml diff --git a/core-java-modules/core-java-lang-oop-types-2/README.md b/core-java-modules/core-java-lang-oop-types-2/README.md new file mode 100644 index 0000000000..70a0273bc4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/README.md @@ -0,0 +1,7 @@ +## Core Java Lang OOP - Types + +This module contains articles about types in Java + +### Relevant Articles: + +- \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-types-2/pom.xml b/core-java-modules/core-java-lang-oop-types-2/pom.xml new file mode 100644 index 0000000000..5370c47279 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/pom.xml @@ -0,0 +1,15 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 0.0.1-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-types-2 + jar + + \ No newline at end of file diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index d44ce97f14..1c36ed73c8 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -91,6 +91,7 @@ core-java-lang-oop-generics core-java-lang-oop-modifiers core-java-lang-oop-types + core-java-lang-oop-types-2 core-java-lang-oop-inheritance core-java-lang-oop-methods core-java-lang-oop-others From d52775ff980103dee4cd64e8966465e33dba3de9 Mon Sep 17 00:00:00 2001 From: mdhtr Date: Fri, 9 Apr 2021 11:32:02 +0200 Subject: [PATCH 2/2] Add examples --- .../core-java-lang-oop-types-2/pom.xml | 7 ++ .../PrimitiveToObjectArrayUnitTest.java | 81 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/conversions/PrimitiveToObjectArrayUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-types-2/pom.xml b/core-java-modules/core-java-lang-oop-types-2/pom.xml index 5370c47279..94a04fe47b 100644 --- a/core-java-modules/core-java-lang-oop-types-2/pom.xml +++ b/core-java-modules/core-java-lang-oop-types-2/pom.xml @@ -12,4 +12,11 @@ core-java-lang-oop-types-2 jar + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/conversions/PrimitiveToObjectArrayUnitTest.java b/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/conversions/PrimitiveToObjectArrayUnitTest.java new file mode 100644 index 0000000000..e0f292f26e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/conversions/PrimitiveToObjectArrayUnitTest.java @@ -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); + } +} \ No newline at end of file