-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamImplExamples.java
More file actions
68 lines (54 loc) · 2.01 KB
/
Copy pathStreamImplExamples.java
File metadata and controls
68 lines (54 loc) · 2.01 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
package Session_18_JavaStreams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamImplExamples {
public static void main(String... args) {
filterOddNumbers();
flatMapExample();
concatenateListOfStrings();
calculateAVG();
}
private static void calculateAVG() {
List<Integer> numbers = Arrays.asList(10, 20, 30, 40);
double avg = numbers.stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0);
System.out.println(avg);
}
private static void concatenateListOfStrings() {
List<String> words = Arrays.asList("apple", "banana", "orange");
String result = words.stream()
.collect(Collectors.joining(", "));
System.out.println(result);
}
private static void flatMapExample() {
List<Person> people = Arrays.asList(
new Person("Bob", Arrays.asList("cititul", "mancatul")),
new Person("Ana", Arrays.asList("plimbare", "gaming"))
);
//Obtinem un stream cu liste de hobiuri
Stream<List<String>> hobbyList = people.stream().map(Person::getHobbies);
// Apelam streamu-ul de liste si il concatenam intr-un stream de strings cu flatMap
Stream<String> hobbies = hobbyList.flatMap(List::stream);
hobbies.forEach(System.out::println);
}
public static void filterOddNumbers() {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
//before
List<Integer> oddNumbers = new ArrayList<>();
for (Integer nr : numbers) {
if (nr % 2 == 0)
oddNumbers.add(nr);
}
System.out.println("Odd numbers: " + oddNumbers);
//@formatter:off
List<Integer> streamOddList = numbers.stream()
.filter(n -> n % 2 == 0)
.toList();
//@formatter:on
}
}