-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreams.java
More file actions
132 lines (95 loc) · 3.73 KB
/
Copy pathStreams.java
File metadata and controls
132 lines (95 loc) · 3.73 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package java8_features;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
/**
* A stream represents a sequence of objects from a source, which supports aggregate operations.
* Pipeline operations.
* Stream operations do the iretations internally.
*/
public class Streams {
public static void main(String[] args) {
Streams streams = new Streams();
streams.simpleExample();
/** FOREACH
* Stream has provided a new method ‘forEach’ to iterate each element of the stream.
* **/
streams.forEachExample();
/** MAP
* The ‘map’ method is used to map each element to its corresponding result.
* **/
streams.mapExample();
/** FILTER
* The ‘filter’ method is used to eliminate elements based on a criteria.
* **/
streams.filterExample();
/** LIMIT
* The ‘limit’ method is used to reduce the size of the stream.
* This code creates a List of random integers.
*/
streams.limitExample();
/** SORTED
* Used to sort the stream.
*/
streams.sortedExample();
}
private void sortedExample() {
System.out.println("Sorting streams");
Random random = new Random();
random.ints(10, 0, 100)
.limit(10)
.sorted()
.forEach(System.out::println);
}
private void limitExample() {
Random random = new Random();
List<Integer> numbers = random.ints(0, 100)
.limit(10)
.boxed()
.collect(Collectors.toList());
numbers.forEach(System.out::println);
}
private void filterExample() {
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
// This code counts the number of empty strings in the String list above.
long count = strings.stream().filter(string -> string.isEmpty()).count();
System.out.println("Number of empty strings: " + count);
}
private void mapExample() {
List<Person> people = Arrays.asList(
new Person("Selgio"),
new Person("Mañel"),
new Person("Lobelto"),
new Person("Coles"),
new Person("Planes"));
// This stream maps the name of the Person in the list people in the new List<String>
List<String> personsNames = people.stream().map(Person::getName).collect(Collectors.toList());
System.out.println("\nPeople's names:");
personsNames.forEach(System.out::println);
}
private void forEachExample() {
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
System.out.println("Before filtering:");
strings.forEach(System.out::println);
System.out.println("After filtering");
filtered.forEach(System.out::println);
}
private void simpleExample() {
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
// This code 'iterates' over the collection and filters it.
// For each string in the collection the filter checks if it is empty,
// if it is not, it is 'added' to a result list
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
}
private static class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}