Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Stream API koans and doc update to make it clear how to run a particular file. #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
==================
Expand Down
68 changes: 68 additions & 0 deletions koans/src/java8/AboutStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String>> 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<String> 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()
Expand All @@ -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()
Expand Down