Joining/Splitting Strings with Java and Stream API (#1130)

* rest with spark java

* 4

* Update Application.java

* indentation changes

* spring @requestmapping shortcuts

* removing spring requestmapping and pushing spring-mvc-java

* Joining/Splitting Strings with Java and Stream API
This commit is contained in:
Abhinab Kanrar 2017-02-08 21:16:10 +05:30 committed by maibin
parent 0bbfa1e10b
commit 7aa774352d
7 changed files with 61 additions and 0 deletions

View File

View File

View File

View File

View File

View File

@ -0,0 +1,23 @@
package com.baeldung.streamApi;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class JoinerSplitter {
public static String join ( String[] arrayOfString ) {
return Arrays.asList(arrayOfString)
.stream()
.map(x -> x)
.collect(Collectors.joining(","));
}
public static List<String> split ( String str ) {
return Stream.of(str.split(","))
.map (elem -> new String(elem))
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.stream;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.baeldung.streamApi.JoinerSplitter;
public class JoinerSplitterTest {
@Test
public void provided_array_convert_to_stream_and_convert_to_string() {
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
String expectation = "java,python,nodejs,ruby";
String result = JoinerSplitter.join(programming_languages);
assertEquals(result, expectation);
}
@Test
public void provided_list_convert_to_stream_and_convert_to_list() {
String programming_languages = "java,python,nodejs,ruby";
List<String> expectation = new ArrayList<String>();
expectation.add("java");
expectation.add("python");
expectation.add("nodejs");
expectation.add("ruby");
List<String> result = JoinerSplitter.split(programming_languages);
assertEquals(result, expectation);
}
}