Refactor Smatt examples (#2091)

This commit is contained in:
Grzegorz Piwowarek 2017-06-15 16:13:51 +02:00 committed by GitHub
parent 586896e5b8
commit b92470e14d
2 changed files with 24 additions and 40 deletions

View File

@ -1,34 +0,0 @@
package com.baeldung.string;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StringToCharStream {
private StringToCharStream() {
String testString = "tests";
IntStream intStream = testString.chars();
IntStream intStream1 = testString.codePoints();
Stream<Character> characterStream = intStream.mapToObj(c -> (char) c);
Stream<Character> characterStream1 = intStream1.mapToObj(c -> (char) c);
System.out.println("Counting Occurrence of Letter");
Map<Character, Integer> map = "Noww".codePoints()
.mapToObj(c -> (char) c)
.filter(Character::isLetter)
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
//printing out the result here
System.out.println(map.toString());
}
public static void main(String[] args) {
new StringToCharStream();
}
}

View File

@ -2,10 +2,14 @@ package com.baeldung.string;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ -16,7 +20,6 @@ public class StringToCharStreamUnitTest {
@Test
public void givenTestString_whenChars_thenReturnIntStream() {
assertThat(testString.chars(), instanceOf(IntStream.class));
}
@ -25,19 +28,34 @@ public class StringToCharStreamUnitTest {
assertThat(testString.codePoints(), instanceOf(IntStream.class));
}
@Test
public void givenTestString_whenCodePoints_thenShowOccurences() throws Exception {
Map<Character, Integer> map = testString.codePoints()
.mapToObj(c -> (char) c)
.filter(Character::isLetter)
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
System.out.println(map);
}
@Test
public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
Stream<Character> characterStream = testString.chars().mapToObj(c -> (char) c);
Stream<Character> characterStream1 = testString.codePoints().mapToObj(c -> (char) c);
Stream<Character> characterStream = testString.chars()
.mapToObj(c -> (char) c);
Stream<Character> characterStream1 = testString.codePoints()
.mapToObj(c -> (char) c);
assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream);
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
}
@Test
public void givenIntStream_whenMapToObj_thenReturnStringStream() {
Stream<String> stringStream
= testString.codePoints().mapToObj(c -> String.valueOf((char) c));
assertNotNull(stringStream);
List<String> strings = testString.codePoints()
.mapToObj(c -> String.valueOf((char) c))
.collect(Collectors.toList());
assertEquals(strings.size(), 5);
}
}