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.
[BAEL-2930] - array to stream conversion (eugenp#6931)
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
core-java-arrays/src/main/java/com/baeldung/array/conversions/StreamArrayConversion.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,52 @@ | ||
package com.baeldung.array.conversions; | ||
|
||
import java.util.Arrays; | ||
import java.util.function.IntFunction; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
public class StreamArrayConversion { | ||
|
||
public static String[] stringStreamToStringArrayUsingFunctionalInterface(Stream<String> stringStream) { | ||
IntFunction<String[]> intFunction = new IntFunction<String[]>() { | ||
@Override | ||
public String[] apply(int value) { | ||
return new String[value]; | ||
} | ||
}; | ||
|
||
return stringStream.toArray(intFunction); | ||
} | ||
|
||
public static String[] stringStreamToStringArrayUsingMethodReference(Stream<String> stringStream) { | ||
return stringStream.toArray(String[]::new); | ||
} | ||
|
||
public static String[] stringStreamToStringArrayUsingLambda(Stream<String> stringStream) { | ||
return stringStream.toArray(value -> new String[value]); | ||
} | ||
|
||
public static Integer[] integerStreamToIntegerArray(Stream<Integer> integerStream) { | ||
return integerStream.toArray(Integer[]::new); | ||
} | ||
|
||
public static int[] intStreamToPrimitiveIntArray(Stream<Integer> integerStream) { | ||
return integerStream.mapToInt(i -> i).toArray(); | ||
} | ||
|
||
public static Stream<String> stringArrayToStreamUsingArraysStream(String[] stringArray) { | ||
return Arrays.stream(stringArray); | ||
} | ||
|
||
public static Stream<String> stringArrayToStreamUsingStreamOf(String[] stringArray) { | ||
return Stream.of(stringArray); | ||
} | ||
|
||
public static IntStream primitiveIntArrayToStreamUsingArraysStream(int[] intArray) { | ||
return Arrays.stream(intArray); | ||
} | ||
|
||
public static Stream<int[]> primitiveIntArrayToStreamUsingStreamOf(int[] intArray) { | ||
return Stream.of(intArray); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...va-arrays/src/test/java/com/baeldung/array/conversions/StreamArrayConversionUnitTest.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,70 @@ | ||
package com.baeldung.array.conversions; | ||
|
||
import static com.baeldung.array.conversions.StreamArrayConversion.intStreamToPrimitiveIntArray; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.integerStreamToIntegerArray; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.stringStreamToStringArrayUsingFunctionalInterface; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.stringStreamToStringArrayUsingLambda; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.stringStreamToStringArrayUsingMethodReference; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.stringArrayToStreamUsingArraysStream; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.stringArrayToStreamUsingStreamOf; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.primitiveIntArrayToStreamUsingArraysStream; | ||
import static com.baeldung.array.conversions.StreamArrayConversion.primitiveIntArrayToStreamUsingStreamOf; | ||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.common.collect.Iterators; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
import org.junit.Test; | ||
|
||
public class StreamArrayConversionUnitTest { | ||
|
||
private String[] stringArray = new String[]{"baeldung", "convert", "to", "string", "array"}; | ||
private Integer[] integerArray = new Integer[]{1, 2, 3, 4, 5, 6, 7}; | ||
private int[] intPrimitiveArray = new int[]{1, 2, 3, 4, 5, 6, 7}; | ||
|
||
@Test | ||
public void givenStringStream_thenConvertToStringArrayUsingFunctionalInterface() { | ||
Stream<String> stringStream = Stream.of("baeldung", "convert", "to", "string", "array"); | ||
assertArrayEquals(stringArray, stringStreamToStringArrayUsingFunctionalInterface(stringStream)); | ||
} | ||
|
||
@Test | ||
public void givenStringStream_thenConvertToStringArrayUsingMethodReference() { | ||
Stream<String> stringStream = Stream.of("baeldung", "convert", "to", "string", "array"); | ||
assertArrayEquals(stringArray, stringStreamToStringArrayUsingMethodReference(stringStream)); | ||
} | ||
|
||
@Test | ||
public void givenStringStream_thenConvertToStringArrayUsingLambda() { | ||
Stream<String> stringStream = Stream.of("baeldung", "convert", "to", "string", "array"); | ||
assertArrayEquals(stringArray, stringStreamToStringArrayUsingLambda(stringStream)); | ||
} | ||
|
||
@Test | ||
public void givenIntegerStream_thenConvertToIntegerArray() { | ||
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7); | ||
assertArrayEquals(integerArray, integerStreamToIntegerArray(integerStream)); | ||
} | ||
|
||
@Test | ||
public void givenIntStream_thenConvertToIntegerArray() { | ||
Stream<Integer> integerStream = IntStream.rangeClosed(1, 7).boxed(); | ||
assertArrayEquals(intPrimitiveArray, intStreamToPrimitiveIntArray(integerStream)); | ||
} | ||
|
||
@Test | ||
public void givenStringArray_whenConvertedTwoWays_thenConvertedStreamsAreEqual() { | ||
assertTrue(Iterators | ||
.elementsEqual(stringArrayToStreamUsingArraysStream(stringArray).iterator(), | ||
stringArrayToStreamUsingStreamOf(stringArray).iterator())); | ||
} | ||
|
||
@Test | ||
public void givenPrimitiveArray_whenConvertedTwoWays_thenConvertedStreamsAreNotEqual() { | ||
assertFalse(Iterators.elementsEqual( | ||
primitiveIntArrayToStreamUsingArraysStream(intPrimitiveArray).iterator(), | ||
primitiveIntArrayToStreamUsingStreamOf(intPrimitiveArray).iterator())); | ||
} | ||
} |