diff --git a/README.md b/README.md index a0519e70..adb8482a 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ https://github.com/matyb/java-koans/archive/master.zip * *gradle*: wrapper for build library used to build koans source, setup project files in eclipse/idea, run tests, etc. you probably don't need to touch anything in here * Change directory to the koans directory: ```cd koans``` * If you are using windows enter: ```run.bat``` or ```./run.sh``` if you are using Mac or Linux +* (Optional) If, rather than starting from the beginning, you'd like to work on a particular topic, you can run the koans startup script like so: ```./run.sh java8.AboutStreams``` (where ```java8``` is the subdirectory, and ```AboutStreams``` is the class you'd like to work on) Developing a Koan: ================== diff --git a/koans/src/java8/AboutStreams.java b/koans/src/java8/AboutStreams.java index ee0c90e4..e83e4812 100644 --- a/koans/src/java8/AboutStreams.java +++ b/koans/src/java8/AboutStreams.java @@ -92,6 +92,62 @@ public void parallelMapReduce() { assertEquals(lengthSum, __); } + @Koan + public void streamFromAnArray() { + int[] ints = {4, 5, 6}; + long numberOfElements = Arrays.stream(ints).count(); + assertEquals(numberOfElements, __); + } + + @Koan + public void allMatch() { + boolean allNonEmpty = places.stream().allMatch(cityName -> !cityName.isEmpty()); + assertEquals(allNonEmpty, __); + } + + @Koan + public void anyMatch() { + boolean anyStartsWithX = places.stream().anyMatch(cityName -> cityName.startsWith("X")); + assertEquals(anyStartsWithX, __); + } + + @Koan + public void streamOf() { + long count = Stream.of("foo", "bar").count(); + assertEquals(count, __); + } + + @Koan + public void distinct() { + long count = Stream.of("foo", "bar", "bar").distinct().count(); + assertEquals(count, __); + } + + @Koan + public void findFirst() { + String first = places.stream().findFirst().get(); + assertEquals(first, __); + } + + @Koan + public void flatMap() { + List> listOfLists = Arrays.asList(Arrays.asList("foo", "bar"), Arrays.asList("one", "two")); + assertEquals(listOfLists.stream().findFirst().get(), __); + Stream streamOfStrings = listOfLists.stream().flatMap(list -> list.stream()); + assertEquals(streamOfStrings.findFirst().get(), __); + } + + List listOfStrings = Arrays.asList("This is the first string.", + "This line follows the first line."); + + @Koan + public void wordCountUsingFlatMap() { + long wordCount = listOfStrings.stream() + // use flatMap method here to count words in listOfStrings + .count(); + assertEquals(wordCount, 11L); + } + @Koan public void limitSkip() { int lengthSum_Limit_3_Skip_1 = places.stream() @@ -102,6 +158,18 @@ public void limitSkip() { assertEquals(lengthSum_Limit_3_Skip_1, __); } + @Koan + public void sorted() { + String second = places.stream().sorted().skip(1).findFirst().get(); + assertEquals(second, __); + } + + @Koan + public void sortedReversed() { + String first = places.stream().sorted(Comparator.reverseOrder()).findFirst().get(); + assertEquals(first, __); + } + @Koan public void lazyEvaluation() { Stream stream = places.stream()