From 4aaefd39df187bd67191c62ac2d7bd0ecc0b83ee Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Sun, 12 Feb 2017 14:20:49 +0530 Subject: [PATCH] changing package structure (#1157) * 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 * adding more join/split functionality * changing package name * testcase change --- core-java/0.5967303215007616 | 0 core-java/0.9252611327674576 | 0 .../com/baeldung/string/JoinerSplitter.java | 36 ++++++++++ .../baeldung/string/JoinerSplitterTest.java | 65 +++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 core-java/0.5967303215007616 create mode 100644 core-java/0.9252611327674576 create mode 100644 core-java/src/main/java/com/baeldung/string/JoinerSplitter.java create mode 100644 core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java diff --git a/core-java/0.5967303215007616 b/core-java/0.5967303215007616 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.9252611327674576 b/core-java/0.9252611327674576 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java b/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java new file mode 100644 index 0000000000..085be66801 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java @@ -0,0 +1,36 @@ +package com.baeldung.string; + +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 String joinWithPrefixPostFix ( 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()); + } + + public static List splitToListOfChar ( String str ) { + return str.chars() + .mapToObj(item -> (char) item) + .collect(Collectors.toList()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java new file mode 100644 index 0000000000..303296cbc6 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java @@ -0,0 +1,65 @@ +package com.baeldung.string; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import com.baeldung.string.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_array_convert_to_stream_and_convert_to_prefixPostfix() { + String[] programming_languages = {"java", "python", + "nodejs", "ruby"}; + String expectation = "[java,python,nodejs,ruby]"; + + String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages); + assertEquals(result, expectation); + } + + @Test + public void provided_string_convert_to_stream_and_convert_to_listOfString() { + 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); + } + + @Test + public void provided_string_convert_to_stream_and_convert_to_listOfChar() { + String programming_languages = "java,python,nodejs,ruby"; + + List expectation = new ArrayList(); + char[] charArray = programming_languages.toCharArray(); + for (char c : charArray) { + expectation.add(c); + } + + List result = JoinerSplitter.splitToListOfChar(programming_languages); + assertEquals(result, expectation); + + } + +}