forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumbers.java
More file actions
51 lines (37 loc) · 1.16 KB
/
Copy pathNumbers.java
File metadata and controls
51 lines (37 loc) · 1.16 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
package modern.challenge;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public final class Numbers {
private Numbers() {
throw new AssertionError("Cannot be instantiated");
}
public static int sumIntegers(List<Integer> integers) {
if (Objects.isNull(integers)) {
throw new IllegalArgumentException("List cannot be null");
}
return integers.stream()
.filter(Objects::nonNull)
.mapToInt(Integer::intValue).sum();
}
public static boolean integersContainsNulls(List<Integer> integers) {
if (Objects.isNull(integers)) {
return false;
}
return integers.stream()
.anyMatch(Objects::isNull);
}
public static List<Integer> evenIntegers(List<Integer> integers) {
if (integers == null) {
return Collections.EMPTY_LIST;
}
List<Integer> evens = new ArrayList<>();
for (Integer nr : integers) {
if (nr != null && nr % 2 == 0) {
evens.add(nr);
}
}
return evens;
}
}