-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamExercises.java
More file actions
executable file
·24 lines (22 loc) · 1007 Bytes
/
Copy pathStreamExercises.java
File metadata and controls
executable file
·24 lines (22 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamExercises {
public int countLines(Path path, int thres) throws IOException{
Stream<String> lines = Files.lines(path);
Stream<String> LessThanThres = lines.filter(line -> line.length() >= thres);
return (int) LessThanThres.count();
}
public List<String> collectWords(Path path) throws IOException{
Stream<String> lines = Files.lines(path);
Stream<String> toLowerCase = lines.map(line -> line.toLowerCase());
Stream<String> indivWords = toLowerCase.map(line-> line.split("[^a-z]+")).flatMap(Arrays::stream);
Stream<String> discEmptyWords = indivWords.filter(word -> word.length() > 0).sorted();
List<String> discConsec = discEmptyWords.distinct().collect(Collectors.toList());
return discConsec;
}
}