Merge pull request #5233 from dkapil/task/BAEL-8901
BAEL-8901 Add section in "Stream operations on Strings" article
This commit is contained in:
commit
9dbdfcf31f
|
@ -2,6 +2,7 @@ package com.baeldung.string;
|
|||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -32,5 +33,12 @@ public class JoinerSplitter {
|
|||
.mapToObj(item -> (char) item)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static Map<String, String> arrayToMap(String[] arrayOfString) {
|
||||
return Arrays.asList(arrayOfString)
|
||||
.stream()
|
||||
.map(str -> str.split(":"))
|
||||
.collect(Collectors.<String[], String, String>toMap(str -> str[0], str -> str[1]));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@ package com.baeldung.string;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -62,5 +64,20 @@ public class JoinerSplitterUnitTest {
|
|||
assertEquals(result, expectation);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringArray_transformedToStream_convertToMap() {
|
||||
|
||||
String[] programming_languages = new String[] {"language:java","os:linux","editor:emacs"};
|
||||
|
||||
Map<String,String> expectation=new HashMap<>();
|
||||
expectation.put("language", "java");
|
||||
expectation.put("os", "linux");
|
||||
expectation.put("editor", "emacs");
|
||||
|
||||
Map<String, String> result = JoinerSplitter.arrayToMap(programming_languages);
|
||||
assertEquals(result, expectation);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue