001package java8;
002
003import com.sandwich.koan.Koan;
004
005import java.util.*;
006import java.util.stream.Collectors;
007import java.util.stream.IntStream;
008import java.util.stream.Stream;
009
010import static com.sandwich.koan.constant.KoanConstants.__;
011import static com.sandwich.util.Assert.assertEquals;
012
013public class AboutStreams {
014
015    String str = "";
016
017    List<String> places = Arrays.asList("Belgrade", "Zagreb", "Sarajevo", "Skopje", "Ljubljana", "Podgorica");
018
019    @Koan
020    public void simpleCount() {
021        long count = places.stream().count();
022        assertEquals(count, __);
023    }
024
025    @Koan
026    public void filteredCount() {
027        long count = places.stream()
028                .filter(s -> s.startsWith("S"))
029                .count();
030        assertEquals(count, __);
031    }
032
033    @Koan
034    public void max() {
035        String longest = places.stream()
036                .max(Comparator.comparing(cityName -> cityName.length()))
037                .get();
038        assertEquals(longest, __);
039    }
040
041    @Koan
042    public void min() {
043        String shortest = places.stream()
044                .min(Comparator.comparing(cityName -> cityName.length()))
045                .get();
046        assertEquals(shortest, __);
047    }
048
049    @Koan
050    public void reduce() {
051        String join = places.stream()
052                .reduce("", String::concat);
053        assertEquals(join, __);
054    }
055
056    @Koan
057    public void reduceWithoutStarterReturnsOptional() {
058        Optional<String> join = places.stream()
059                .reduce(String::concat);
060        assertEquals(join.get(), __);
061    }
062
063    @Koan
064    public void join() {
065        String join = places.stream()
066                .reduce((accumulated, cityName) -> accumulated + "\", \"" + cityName)
067                .get();
068        assertEquals(join, __);
069    }
070
071    @Koan
072    public void reduceWithBinaryOperator() {
073        String join = places.stream()
074                .reduce("", String::concat);
075        assertEquals(join, __);
076    }
077
078    @Koan
079    public void stringJoin() {
080        String join = places.stream()
081                .collect(Collectors.joining("\", \""));
082        assertEquals(join, __);
083    }
084
085    @Koan
086    public void mapReduce() {
087        OptionalDouble averageLengthOptional = places.stream()
088                .mapToInt(String::length)
089                .average();
090        double averageLength = Math.round(averageLengthOptional.getAsDouble());
091        assertEquals(averageLength, __);
092    }
093
094    @Koan
095    public void parallelMapReduce() {
096        int lengthSum = places.parallelStream()
097                .mapToInt(String::length)
098                .sum();
099        assertEquals(lengthSum, __);
100    }
101
102    @Koan
103    public void limitSkip() {
104        int lengthSum_Limit_3_Skip_1 = places.stream()
105                .mapToInt(String::length)
106                .limit(3)
107                .skip(1)
108                .sum();
109        assertEquals(lengthSum_Limit_3_Skip_1, __);
110    }
111
112    @Koan
113    public void lazyEvaluation() {
114        Stream stream = places.stream()
115                .filter(s -> {
116                    str = "hello";
117                    return s.startsWith("S");
118                });
119        assertEquals(str, __);
120    }
121
122    @Koan
123    public void sumRange() {
124        int sum = IntStream.range(1, 4).sum();
125        assertEquals(sum, __);
126    }
127
128    @Koan
129    public void rangeToList() {
130        List<Integer> range = IntStream.range(1, 4)
131                .boxed()
132                .collect(Collectors.toList());
133        assertEquals(range, __);
134    }
135}