Merge pull request #9419 from alexbaeldung/tutorial/Convert-an-array-of-primitives
feat: Convert an array of primitives to a List
This commit is contained in:
commit
44fd4f91b4
|
@ -27,6 +27,11 @@
|
|||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.10</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.collections.iterators;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
public class ConvertPrimitivesArrayToList {
|
||||
|
||||
public static void failConvert() {
|
||||
int[] input = new int[]{1,2,3,4};
|
||||
// List<Integer> inputAsList = Arrays.asList(input);
|
||||
}
|
||||
|
||||
public static List<Integer> iterateConvert(int[] input) {
|
||||
List<Integer> output = new ArrayList<Integer>();
|
||||
for (int value : input) {
|
||||
output.add(value);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static List<Integer> streamConvert(int[] input) {
|
||||
List<Integer> output = Arrays.stream(input).boxed().collect(Collectors.toList());
|
||||
return output;
|
||||
}
|
||||
|
||||
public static List<Integer> streamConvertIntStream(int[] input) {
|
||||
List<Integer> output = IntStream.of(input).boxed().collect(Collectors.toList());
|
||||
return output;
|
||||
}
|
||||
|
||||
public static List<Integer> guavaConvert(int[] input) {
|
||||
List<Integer> output = Ints.asList(input);
|
||||
return output;
|
||||
}
|
||||
|
||||
public static List<Integer> apacheCommonConvert(int[] input) {
|
||||
Integer[] outputBoxed = ArrayUtils.toObject(input);
|
||||
List<Integer> output = Arrays.asList(outputBoxed);
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.collections.iterators;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ConvertPrimitivesArrayToListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenArrayWithPrimitives_whenIterativeConvert_thenArrayGetsConverted() {
|
||||
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.iterateConvert(new int[]{1,2,3,4}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithPrimitives_whenStreamConvert_thenArrayGetsConverted() {
|
||||
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.streamConvert(new int[]{1,2,3,4}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithPrimitives_whenIntStreamConvert_thenArrayGetsConverted() {
|
||||
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.streamConvertIntStream(new int[]{1,2,3,4}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithPrimitives_whenGuavaConvert_thenArrayGetsConverted() {
|
||||
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.guavaConvert(new int[]{1,2,3,4}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithPrimitives_whenApacheCommonConvert_thenArrayGetsConverted() {
|
||||
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.apacheCommonConvert(new int[]{1,2,3,4}));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue