[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:
parent
b40a061969
commit
9d3667ac77
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.streams.streamtoiterable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import joptsimple.internal.Strings;
|
||||||
|
|
||||||
|
public class StreamToIterable {
|
||||||
|
public String streamToIterableLambda(List<String> listOfStrings) {
|
||||||
|
Stream<String> stringStream = listOfStrings.stream();
|
||||||
|
StringBuilder sentence = new StringBuilder();
|
||||||
|
for (String eachString : (Iterable<String>) () -> stringStream.iterator()) {
|
||||||
|
doSomethingOnString(eachString, sentence);
|
||||||
|
}
|
||||||
|
return sentence.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String streamToIterableMethodReference(List<String> listOfStrings) {
|
||||||
|
Stream<String> stringStream = listOfStrings.stream();
|
||||||
|
StringBuilder sentence = new StringBuilder();
|
||||||
|
for (String eachString : (Iterable<String>) stringStream::iterator) {
|
||||||
|
doSomethingOnString(eachString, sentence);
|
||||||
|
}
|
||||||
|
return sentence.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String streamToList(List<String> listOfStrings) {
|
||||||
|
Stream<String> stringStream = listOfStrings.stream();
|
||||||
|
StringBuilder sentence = new StringBuilder();
|
||||||
|
for (String eachString : stringStream.collect(Collectors.toList())) {
|
||||||
|
doSomethingOnString(eachString, sentence);
|
||||||
|
}
|
||||||
|
return sentence.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doSomethingOnString(String s, StringBuilder sentence) {
|
||||||
|
if (!Strings.isNullOrEmpty(s)) {
|
||||||
|
sentence.append(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue