Skip to content

Commit 5871bf7

Browse files
committed
more stream operations
1 parent 9983ecf commit 5871bf7

1 file changed

Lines changed: 80 additions & 2 deletions

File tree

‎koans/src/java8/AboutStreams.java‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import com.sandwich.koan.Koan;
44

5-
import java.util.Arrays;
6-
import java.util.List;
5+
import java.util.*;
76
import java.util.stream.Collectors;
87
import java.util.stream.IntStream;
98
import java.util.stream.Stream;
@@ -31,6 +30,85 @@ public void filteredCount() {
3130
assertEquals(count, __);
3231
}
3332

33+
@Koan
34+
public void max(){
35+
String longest = places.stream()
36+
.max(Comparator.comparing(cityName -> cityName.length()))
37+
.get();
38+
assertEquals(longest, "");
39+
}
40+
41+
@Koan
42+
public void min(){
43+
String longest = places.stream()
44+
.max(Comparator.comparing(cityName -> cityName.length()))
45+
.get();
46+
assertEquals(longest, "");
47+
}
48+
49+
@Koan
50+
public void reduceWithoutFirstElementReturnsOptional(){
51+
Optional<String> join = places.stream()
52+
.reduce(String::concat);
53+
assertEquals(join.get(), "");
54+
}
55+
56+
@Koan
57+
public void reduce(){
58+
String join = places.stream()
59+
.reduce("", String::concat);
60+
assertEquals(join, "");
61+
}
62+
63+
@Koan
64+
public void join(){
65+
String join = places.stream()
66+
.reduce((accumulated, cityName) -> accumulated + "\", \"" + cityName)
67+
.get();
68+
assertEquals(join, "");
69+
}
70+
71+
@Koan
72+
public void reduceWithBinaryOperator(){
73+
String join = places.stream()
74+
.reduce("", String::concat);
75+
assertEquals(join, "");
76+
}
77+
78+
@Koan
79+
public void stringJoin(){
80+
String join = places.stream()
81+
.collect(Collectors.joining("\", \""));
82+
assertEquals(join, __);
83+
}
84+
85+
@Koan
86+
public void mapReduce(){
87+
OptionalDouble averageLengthOptional = places.stream()
88+
.mapToInt(String::length)
89+
.average();
90+
double averageLength = Math.round(averageLengthOptional.getAsDouble());
91+
assertEquals(averageLength, __);
92+
}
93+
94+
@Koan
95+
public void parallelMapReduce(){
96+
int lengthSum = places.parallelStream()
97+
.mapToInt(String::length)
98+
.sum();
99+
assertEquals(lengthSum, __);
100+
}
101+
102+
@Koan
103+
public void limitSkip(){
104+
int lengthSum_Limit_5_Skip_1 = places.stream()
105+
.mapToInt(String::length)
106+
.limit(5)
107+
.skip(1)
108+
.sum();
109+
assertEquals(lengthSum_Limit_5_Skip_1, __);
110+
}
111+
34112
@Koan
35113
public void lazyEvaluation() {
36114
Stream stream = places.stream()

0 commit comments

Comments
 (0)