[JAVA-5783] stream to iterable (#13369)

* [JAVA-5783] stream to iterable

* [JAVA-5783] tests refactor

---------

Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
Bhaskar Ghosh Dastidar
2023-01-31 08:16:11 +05:30
committed by GitHub
parent 2113ddf953
commit a58eb8abf4
2 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package com.baeldung.streams.streamtoiterable;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class StreamToIterableUnitTest {
@Test
public void givenList_whenLambdaIsUsed_ThenStreamAsIterable(){
StreamToIterable streamToIterable = new StreamToIterable();
String actualString = streamToIterable.streamToIterableLambda(getListOfStrings());
String expectedString = "Thisisasentencewithnospaces";
Assert.assertEquals(expectedString, actualString);
}
@Test
public void givenList_whenMethodReferenceIsUsed_ThenStreamAsIterable(){
StreamToIterable streamToIterable = new StreamToIterable();
String actualString = streamToIterable.streamToIterableMethodReference(getListOfStrings());
String expectedString = "Thisisasentencewithnospaces";
Assert.assertEquals(expectedString, actualString);
}
@Test
public void givenList_whenCollectedToList_ThenStreamAsIterable(){
StreamToIterable streamToIterable = new StreamToIterable();
String actualString = streamToIterable.streamToList(getListOfStrings());
String expectedString = "Thisisasentencewithnospaces";
Assert.assertEquals(expectedString, actualString);
}
private List<String> getListOfStrings(){
List<String> listOfStrings = new ArrayList<>();
listOfStrings.add("This");
listOfStrings.add("is");
listOfStrings.add("a");
listOfStrings.add(null);
listOfStrings.add("sentence");
listOfStrings.add("with");
listOfStrings.add("no");
listOfStrings.add(null);
listOfStrings.add("spaces");
return listOfStrings;
}
}