forked from next-step/java-blackjack-playground
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamStudy.java
More file actions
47 lines (38 loc) · 1.71 KB
/
StreamStudy.java
File metadata and controls
47 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package nextstep.fp;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class StreamStudy {
public static long countWords() throws IOException {
String contents = new String(Files.readAllBytes(Paths
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));
long count = 0;
for (String w : words) {
if (w.length() > 12) count++;
}
return count;
}
public static void printLongestWordTop100() throws IOException {
String contents = new String(Files.readAllBytes(Paths
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));
// TODO 이 부분에 구현한다.
words.stream().filter(w -> w.length() > 12).sorted(Comparator.comparing(String::length).reversed()).distinct().limit(100).map(String::toLowerCase).forEach(System.out::println);
}
public static List<Integer> doubleNumbers(List<Integer> numbers) {
return numbers.stream().map(x -> 2 * x).collect(Collectors.toList());
}
public static long sumAll(List<Integer> numbers) {
return numbers.stream().reduce(0, (x, y) -> x + y);
}
public static long sumOverThreeAndDouble(List<Integer> numbers) {
return doubleNumbers(numbers.stream().filter(x -> x>3).collect(Collectors.toList()))
.stream().reduce(Integer::sum).get();
}
}