|
2 | 2 |
|
3 | 3 | import com.sandwich.koan.Koan; |
4 | 4 |
|
5 | | -import java.util.Arrays; |
6 | | -import java.util.List; |
| 5 | +import java.util.*; |
7 | 6 | import java.util.stream.Collectors; |
8 | 7 | import java.util.stream.IntStream; |
9 | 8 | import java.util.stream.Stream; |
@@ -31,6 +30,85 @@ public void filteredCount() { |
31 | 30 | assertEquals(count, __); |
32 | 31 | } |
33 | 32 |
|
| 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 | + |
34 | 112 | @Koan |
35 | 113 | public void lazyEvaluation() { |
36 | 114 | Stream stream = places.stream() |
|
0 commit comments