-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamsTest.java
71 lines (61 loc) · 2.24 KB
/
StreamsTest.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.rajpro;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamsTest {
private enum Status {
OPEN, CLOSED
};
private static final class Task {
private final Status status;
private final Integer points;
Task(final Status status, final Integer points) {
this.status = status;
this.points = points;
}
public Integer getPoints() {
return points;
}
public Status getStatus() {
return status;
}
@Override
public String toString() {
return String.format("[%s, %d]", status, points);
}
}
public static void main(String[] args) throws Exception {
final Collection<Task> tasks = Arrays.asList(new Task(Status.OPEN, 5), new Task(Status.OPEN, 13),
new Task(Status.CLOSED, 8));
final long totalPointsOfOpenTasks = tasks.stream().filter(task -> task.getStatus() == Status.OPEN)
.mapToInt(Task::getPoints).sum();
System.out.println("Total points: " + totalPointsOfOpenTasks);
System.out.println("************************************");
System.out.println("Group tasks by status");
final Map<Status, List<Task>> map = tasks.stream().collect(Collectors.groupingBy(Task::getStatus));
System.out.println(map);
System.out.println("**************************************");
int totalPoints = 3;
final Collection<String> result = tasks.stream() // Stream< String >
.mapToInt(Task::getPoints) // IntStream
.asLongStream() // LongStream
.mapToDouble(points -> points / totalPoints) // DoubleStream
.boxed() // Stream< Double >
.mapToLong(weigth -> (long) (weigth * 100)) // LongStream
.mapToObj(percentage -> percentage + "%") // Stream< String>
.collect(Collectors.toList()); // List< String >
System.out.println(result);
System.out.println("**************************************************");
final Path path = new File( "H:\\Preparation\\gitlearning.txt").toPath();
try( Stream< String > lines = Files.lines( path, StandardCharsets.UTF_8 ) ) {
lines.onClose( () -> System.out.println("Done!") ).forEach( System.out::println );
}
}
}