Skip to content

Commit

Permalink
[BAEL-2930] - array to stream conversion (eugenp#6931)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdjole authored and maibin committed May 11, 2019
1 parent 734692d commit 61890eb
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
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);
}
}
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()));
}
}

0 comments on commit 61890eb

Please sign in to comment.