From 7aa774352de97555217eb9e18c0db27e633fbe66 Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Wed, 8 Feb 2017 21:16:10 +0530 Subject: [PATCH] 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 --- core-java/0.004102810554955205 | 0 core-java/0.04832801936270381 | 0 core-java/0.5633433244738808 | 0 core-java/0.6256429734439612 | 0 core-java/0.9799201796740292 | 0 .../baeldung/streamApi/JoinerSplitter.java | 23 +++++++++++ .../baeldung/stream/JoinerSplitterTest.java | 38 +++++++++++++++++++ 7 files changed, 61 insertions(+) create mode 100644 core-java/0.004102810554955205 create mode 100644 core-java/0.04832801936270381 create mode 100644 core-java/0.5633433244738808 create mode 100644 core-java/0.6256429734439612 create mode 100644 core-java/0.9799201796740292 create mode 100644 core-java/src/main/java/com/baeldung/streamApi/JoinerSplitter.java create mode 100644 core-java/src/test/java/com/baeldung/stream/JoinerSplitterTest.java diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/src/main/java/com/baeldung/streamApi/JoinerSplitter.java b/core-java/src/main/java/com/baeldung/streamApi/JoinerSplitter.java new file mode 100644 index 0000000000..5183921dea --- /dev/null +++ b/core-java/src/main/java/com/baeldung/streamApi/JoinerSplitter.java @@ -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 split ( String str ) { + return Stream.of(str.split(",")) + .map (elem -> new String(elem)) + .collect(Collectors.toList()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/stream/JoinerSplitterTest.java b/core-java/src/test/java/com/baeldung/stream/JoinerSplitterTest.java new file mode 100644 index 0000000000..19d945f836 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/stream/JoinerSplitterTest.java @@ -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 expectation = new ArrayList(); + expectation.add("java"); + expectation.add("python"); + expectation.add("nodejs"); + expectation.add("ruby"); + + List result = JoinerSplitter.split(programming_languages); + + assertEquals(result, expectation); + } + +}