-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathStreamStudy.java
More file actions
50 lines (41 loc) · 1.71 KB
/
StreamStudy.java
File metadata and controls
50 lines (41 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
48
49
50
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}]+"));
return words.stream()
.filter(w -> w.length() > 12)
.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}]+"));
words.stream()
.filter(w -> w.length() > 12) // 단어 길이 12 이상
.sorted((o1, o2) -> o2.length() - o1.length()) // 긴 순서
.distinct()
.forEach(s -> System.out.println(s.toLowerCase()));
}
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 numbers.stream()
.filter(i -> i > 3)
.map(i -> 2 * i)
.reduce(0, (x, y) -> x + y);
}
}