added updated example codes (#2062)
This commit is contained in:
parent
f73893bbb9
commit
53f4ec5f87
@ -15,8 +15,18 @@ public class StringToCharStream {
|
|||||||
//let's use the Stream API to manipulate a string
|
//let's use the Stream API to manipulate a string
|
||||||
//this will count the occurrence of each character in the test string
|
//this will count the occurrence of each character in the test string
|
||||||
|
|
||||||
|
String testString = "tests";
|
||||||
|
|
||||||
|
//first get an IntStream
|
||||||
|
IntStream intStream = testString.chars();
|
||||||
|
IntStream intStream1 = testString.codePoints();
|
||||||
|
|
||||||
|
//now let's map them
|
||||||
|
Stream<Character> characterStream = intStream.mapToObj(c -> (char) c);
|
||||||
|
Stream<Character> characterStream1 = intStream1.mapToObj(c -> (char) c);
|
||||||
|
|
||||||
System.out.println("Counting Occurrence of Letter");
|
System.out.println("Counting Occurrence of Letter");
|
||||||
String testString = "Noww";
|
testString = "Noww";
|
||||||
|
|
||||||
//we don't want to use foreach, so . . .
|
//we don't want to use foreach, so . . .
|
||||||
|
|
||||||
@ -35,6 +45,7 @@ public class StringToCharStream {
|
|||||||
|
|
||||||
//printing out the result here
|
//printing out the result here
|
||||||
System.out.println(map.toString());
|
System.out.println(map.toString());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,7 +5,9 @@ import org.junit.Test;
|
|||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,12 +19,13 @@ public class StringToCharStreamUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTestString_whenChars_thenReturnIntStream() {
|
public void givenTestString_whenChars_thenReturnIntStream() {
|
||||||
assertTrue(testString.chars() instanceof IntStream);
|
|
||||||
|
assertThat(testString.chars(), instanceOf(IntStream.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTestString_whenCodePoints_thenReturnIntStream() {
|
public void givenTestString_whenCodePoints_thenReturnIntStream() {
|
||||||
assertTrue(testString.codePoints() instanceof IntStream);
|
assertThat(testString.codePoints(), instanceOf(IntStream.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -33,4 +36,11 @@ public class StringToCharStreamUnitTest {
|
|||||||
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user