From 1535f8fa942734489c4b32b1d09cb27964391888 Mon Sep 17 00:00:00 2001 From: estukalov Date: Wed, 17 May 2023 18:23:42 -0700 Subject: [PATCH 1/3] More Stream API koans and readme addition. --- koans/README.md | 3 ++ koans/src/java8/AboutStreams.java | 68 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/koans/README.md b/koans/README.md index 044f12ce..1b16513b 100755 --- a/koans/README.md +++ b/koans/README.md @@ -9,3 +9,6 @@ https://github.com/matyb/java-koans/archive/master.zip * Eclipse ```./gradlew eclipse (WINDOWS: gradlew.bat eclipse)``` * Back to the console and run ```./run.sh``` (WINDOWS: ```run.bat```) * Follow the instructions and start hacking +* (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) diff --git a/koans/src/java8/AboutStreams.java b/koans/src/java8/AboutStreams.java index ee0c90e4..dbfeaa52 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() From a24137de9369b21211d9d53febd086ca3f35e9a8 Mon Sep 17 00:00:00 2001 From: estukalov Date: Wed, 17 May 2023 18:44:01 -0700 Subject: [PATCH 2/3] Doc change moved to the correct place, and minor formatting. --- README.md | 3 +++ koans/README.md | 3 --- koans/src/java8/AboutStreams.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a0519e70..3bf33e13 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,9 @@ 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/README.md b/koans/README.md index 1b16513b..044f12ce 100755 --- a/koans/README.md +++ b/koans/README.md @@ -9,6 +9,3 @@ https://github.com/matyb/java-koans/archive/master.zip * Eclipse ```./gradlew eclipse (WINDOWS: gradlew.bat eclipse)``` * Back to the console and run ```./run.sh``` (WINDOWS: ```run.bat```) * Follow the instructions and start hacking -* (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) diff --git a/koans/src/java8/AboutStreams.java b/koans/src/java8/AboutStreams.java index dbfeaa52..e83e4812 100644 --- a/koans/src/java8/AboutStreams.java +++ b/koans/src/java8/AboutStreams.java @@ -94,14 +94,14 @@ public void parallelMapReduce() { @Koan public void streamFromAnArray() { - int[] ints = {4,5,6}; + 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()); + boolean allNonEmpty = places.stream().allMatch(cityName -> !cityName.isEmpty()); assertEquals(allNonEmpty, __); } From 057b017ff7b06532a11097690be90e1fe4eca9a8 Mon Sep 17 00:00:00 2001 From: estukalov Date: Wed, 17 May 2023 18:50:35 -0700 Subject: [PATCH 3/3] Another doc change to hopefully correct the formatting. --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 3bf33e13..adb8482a 100644 --- a/README.md +++ b/README.md @@ -14,9 +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) +* (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: ==================