Merge pull request #10643 from mdhtr/mdhtr/BAEL-4179_primitive_to_object_array
BAEL-4179 Cast primitive type array into object array in java
This commit is contained in:
		
						commit
						31b48566d3
					
				
							
								
								
									
										7
									
								
								core-java-modules/core-java-lang-oop-types-2/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								core-java-modules/core-java-lang-oop-types-2/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| ## Core Java Lang OOP - Types | ||||
| 
 | ||||
| This module contains articles about types in Java | ||||
| 
 | ||||
| ### Relevant Articles:  | ||||
| 
 | ||||
| - | ||||
							
								
								
									
										22
									
								
								core-java-modules/core-java-lang-oop-types-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								core-java-modules/core-java-lang-oop-types-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -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> | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -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> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user