-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjIntConsumerEx.java
More file actions
61 lines (47 loc) · 1.47 KB
/
Copy pathObjIntConsumerEx.java
File metadata and controls
61 lines (47 loc) · 1.47 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
package predefinedFunInterfaces;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.ObjIntConsumer;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
public class ObjIntConsumerEx {
public static void main(String[] args) {
// objIntConsumerEx();
// toIntFunctionEx();
removeIfSample();
}
private static void removeIfSample() {
List<Integer> arr = new ArrayList<>();
// Arrays.asList(9, 2, 5, 7, 4); // do not have remove() method - return type of
// asList is Arrays&ArrayList not java.util.ArrayList
arr.add(9);
arr.add(2);
arr.add(5);
arr.add(7);
arr.add(4);
Predicate<Integer> pred1 = p -> (p > 2);
Predicate<Integer> pred2 = pred1.and(p -> (p < 4));
System.out.println(arr.removeIf(pred2));
for (Integer integer : arr) { // Concurrent Modification - fail fast
arr.remove(0);
}
}
private static void objIntConsumerEx() {
List<Integer> arr = Arrays.asList(3, 2, 5, 7, 4);
ObjIntConsumer<List<Integer>> func = (ar, n) -> {
ar.stream().forEach(x -> System.out.print(x + 1 + " "));
};
func.accept(arr, 2);
System.out.println();
}
private static void toIntFunctionEx() {
Stream<Double> listStr = Stream.of(10.0, 20.3, 30.4);
ToIntFunction<Double> fun = d -> d.intValue();
Stream<Integer> boxed = listStr.mapToInt(fun).boxed();
boxed.forEach(System.out::print);
System.out.println();
}
}