BAEL-6100-Add-example-code-for-integer-array-to-strings-using-streams… (#13375)
* BAEL-6100-Add-example-code-for-integer-array-to-strings-using-streams-tutorial * BAEL-6100 remove wrongly added README update * BAEL-6100 Add interger array to single string example * BAEL-6100 Correct typo in test class name
This commit is contained in:
parent
4acdb03e61
commit
92864107b9
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.streams.intarraytostrings;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class ArrayConversionUtils {
|
||||||
|
|
||||||
|
public static void createStreamExample() {
|
||||||
|
int[] intArray = { 1, 2, 3, 4, 5 };
|
||||||
|
IntStream intStream = Arrays.stream(intArray);
|
||||||
|
|
||||||
|
Integer[] integerArray = { 1, 2, 3, 4, 5 };
|
||||||
|
Stream<Integer> integerStream = Arrays.stream(integerArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[] convertToStringArray(Integer[] input) {
|
||||||
|
return Arrays.stream(input)
|
||||||
|
.map(Object::toString)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[] convertToStringArray(int[] input) {
|
||||||
|
return Arrays.stream(input)
|
||||||
|
.mapToObj(Integer::toString)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[] convertToStringArrayWithBoxing(int[] input) {
|
||||||
|
return Arrays.stream(input)
|
||||||
|
.boxed()
|
||||||
|
.map(Object::toString)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String convertToString(int[] input){
|
||||||
|
return Arrays.stream(input)
|
||||||
|
.mapToObj(Integer::toString)
|
||||||
|
.collect(Collectors.joining(", "));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.baeldung.streams.intarraytostrings;
|
||||||
|
|
||||||
|
import static com.baeldung.streams.intarraytostrings.ArrayConversionUtils.convertToString;
|
||||||
|
import static com.baeldung.streams.intarraytostrings.ArrayConversionUtils.convertToStringArray;
|
||||||
|
import static com.baeldung.streams.intarraytostrings.ArrayConversionUtils.convertToStringArrayWithBoxing;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class IntArrayToStringUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenConvertingIntegers_thenHandleStreamOfIntegers() {
|
||||||
|
Integer[] integerNumbers = { 1, 2, 3, 4, 5 };
|
||||||
|
String[] expectedOutput = { "1", "2", "3", "4", "5" };
|
||||||
|
|
||||||
|
String[] strings = convertToStringArray(integerNumbers);
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(expectedOutput, strings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenConvertingInts_thenHandleIntStream() {
|
||||||
|
int[] intNumbers = { 1, 2, 3, 4, 5 };
|
||||||
|
String[] expectedOutput = { "1", "2", "3", "4", "5" };
|
||||||
|
|
||||||
|
String[] strings = convertToStringArray(intNumbers);
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(expectedOutput, strings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnIntArray_whenBoxingToInteger_thenHandleStreamOfIntegers() {
|
||||||
|
int[] intNumbers = { 1, 2, 3, 4, 5 };
|
||||||
|
String[] expectedOutput = { "1", "2", "3", "4", "5" };
|
||||||
|
|
||||||
|
String[] strings = convertToStringArrayWithBoxing(intNumbers);
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(expectedOutput, strings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnIntArray_whenUsingCollectorsJoining_thenReturnCommaSeparatedString(){
|
||||||
|
int[] intNumbers = { 1, 2, 3, 4, 5 };
|
||||||
|
String expectedOutput = "1, 2, 3, 4, 5";
|
||||||
|
|
||||||
|
String string = convertToString(intNumbers);
|
||||||
|
|
||||||
|
Assert.assertEquals(expectedOutput, string);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user