-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectorsExample.java
More file actions
41 lines (32 loc) · 1.18 KB
/
CollectorsExample.java
File metadata and controls
41 lines (32 loc) · 1.18 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
package part2streams;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) throws IOException {
List<Person> persons = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(CollectorsExample.class.getResourceAsStream("persons.txt")))) {
Stream<String> lines = reader.lines();
lines.map(line -> {
String[] a = line.split(" ");
Person p = new Person(a[0].trim(), Integer.valueOf(a[1]));
persons.add(p);
return p;
}).forEach(System.out::println);
}
System.out.println("________");
Stream<Person> stream = persons.stream();
stream.filter(p -> p.getAge() > 20).forEach(System.out::println);
System.out.println("________");
stream = persons.stream();
Optional<Person> min = stream.filter(p -> p.getAge() > 20).min(Comparator.comparing(Person::getAge));
System.out.println(min);
System.out.println("________");
}
}