-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamEx5.java
More file actions
33 lines (25 loc) · 854 Bytes
/
StreamEx5.java
File metadata and controls
33 lines (25 loc) · 854 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
25
26
27
28
29
30
31
32
33
package com.java8.streams;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class StreamEx5 {
public static void main(String[] args) throws IOException {
Files.list(Paths.get("."))
.map(Path:: getFileName)
// peek is basically to do additional job for each iteration
.peek(e -> System.out.println("Filtered value: " + e))
.forEach( file -> someThingForFile(file));
System.out.println("---------------------------------");
Files.list(Paths.get("."))
.map(Path::getFileName)
.map(Path:: toString)
.filter(name -> name.endsWith(".java"))
.limit(3)
.sorted()
.forEach(System.out :: println);
}
public static void someThingForFile(Path file){
System.out.println("File is " + file.getFileName());
}
}