-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctional2.java
More file actions
73 lines (64 loc) · 2.61 KB
/
Functional2.java
File metadata and controls
73 lines (64 loc) · 2.61 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
import java.util.List;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class Functional2 {
/**
* Given a list of integers, return a list of the integers, omitting any that are less than 0.
*/
public List<Integer> noNeg(List<Integer> nums) {
return nums.stream().filter(n -> n >= 0).collect(Collectors.toList());
}
/**
* Given a list of non-negative integers, return a list of those numbers except omitting any that end with 9.
* (Note: % by 10)
*/
public List<Integer> no9(List<Integer> nums) {
return nums.stream().filter(n -> n % 10 < 9).collect(Collectors.toList());
}
/**
* Given a list of integers, return a list of those numbers, omitting any that are between 13 and 19 inclusive.
*/
public List<Integer> noTeen(List<Integer> nums) {
return nums.stream().filter(n -> n < 13 || n > 19).collect(Collectors.toList());
}
/**
* Given a list of strings, return a list of the strings, omitting any string that contains a "z".
* (Note: the str.contains(x) method returns a boolean)
*/
public List<String> noZ(List<String> strings) {
return strings.stream().filter(s -> !s.contains("z")).collect(Collectors.toList());
}
/**
* Given a list of strings, return a list of the strings, omitting any string length 4 or more.
*/
public List<String> noLong(List<String> strings) {
return strings.stream().filter(s -> s.length() < 4).collect(Collectors.toList());
}
/**
* Given a list of strings, return a list of the strings, omitting any string length 3 or 4.
*/
public List<String> no34(List<String> strings) {
return strings.stream().filter(s -> s.length() < 3 || s.length() > 4).collect(Collectors.toList());
}
/**
* Given a list of strings, return a list where each string has "y" added at its end,
* omitting any resulting strings that contain "yy" as a substring anywhere.
*/
public List<String> noYY(List<String> strings) {
return strings.stream().map(s -> s + "y").filter(s -> !s.contains("yy")).collect(Collectors.toList());
}
/**
* Given a list of non-negative integers, return a list of those numbers multiplied by 2,
* omitting any of the resulting numbers that end in 2.
*/
public List<Integer> two2(List<Integer> nums) {
return nums.stream().map(n -> n * 2).filter(n -> n % 10 != 2).collect(Collectors.toList());
}
/**
* Given a list of integers, return a list of those numbers squared and the product added to 10,
* omitting any of the resulting numbers that end in 5 or 6.
*/
public List<Integer> square56(List<Integer> nums) {
return nums.stream().map(n -> n * n + 10).filter(n -> (n %= 10) > 6 || n < 5).collect(Collectors.toList());
}
}