-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamExample.java
More file actions
136 lines (97 loc) · 3.1 KB
/
StreamExample.java
File metadata and controls
136 lines (97 loc) · 3.1 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
133
134
135
136
package com.java8.examples;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class StreamExample {
public static void main(String[] args) {
Runnable run = null;
boolean isPresent = false;
List<String> strCollection = new ArrayList<>();
strCollection.add("riaan");
strCollection.add("nitin");
strCollection.add("pratik");
strCollection.add("subin");
strCollection.add("pankaj");
strCollection.add("amit");
strCollection.add("sushil");
// filter
run = () -> System.out.println("Filter Demo");
run.run();
strCollection
.stream()
.filter((s) -> s.startsWith("s"))
.forEach(System.out::println);
//Sorted
run = () -> System.out.println("Sorted Demo");
run.run();
strCollection
.stream()
.sorted()
.filter((s) -> s.startsWith("p"))
.forEach(System.out::println);
//MAP
run = () -> System.out.println("Map Demo");
run.run();
strCollection
.stream()
.map(String::toUpperCase)
.sorted((a, b) -> b.compareTo(a))
.forEach(System.out::println);
//Match
run = () -> System.out.println("Match Demo");
run.run();
isPresent = strCollection
.stream()
.anyMatch((s) -> s.startsWith("n"));
System.out.println("Any Name in collection starting with char 'n' are present: " + isPresent);
isPresent = strCollection
.stream()
.allMatch((s) -> s.startsWith("n"));
System.out.println("All Names in collection starting with char 'n' are present: " + isPresent);
isPresent = strCollection
.stream()
.noneMatch((s) -> s.startsWith("z"));
System.out.println("No Names in collection starting with char 'y' are present: " + isPresent);
//COUNT
run = () -> System.out.println("COUNT Demo");
run.run();
long count = strCollection
.stream().filter((s) -> s.startsWith("p"))
.count();
System.out.println("Total Names in collection starting with char 'p': " + count);
//Reduce
run = () -> System.out.println("Reduce Demo");
run.run();
Optional<String> reduced =
strCollection
.stream()
.sorted()
.reduce((s1, s2) -> s1 + "#" + s2);
reduced.ifPresent(System.out::println);
//Map
Map<Integer, String> map = new HashMap<>();
for(int i=0 ; i < 10; i++) {
map.putIfAbsent(i, "val" + i);
}
map.forEach((id, val) -> System.out.println(val));
map.computeIfPresent(3, (num, val) -> val + num);
System.out.println(map.get(3));
map.computeIfPresent(9, (num , val) -> null);
System.out.println(map.containsKey(9));
map.computeIfAbsent(23, num -> "val" + num);
System.out.println(map.containsKey(23));
map.computeIfAbsent(3, num -> "bam");
System.out.println(map.get(3));
map.remove(3, "val3");
System.out.println(map.get(3));
map.remove(3, "val33");
System.out.println(map.get(3));
System.out.println(map.getOrDefault(42, "not found"));
map.merge(9, "val9", (value, newValue) -> value.concat(newValue));
System.out.println(map.get(9));
map.merge(9, "concat", (value, newValue) -> value.concat(newValue));
System.out.println(map.get(9));
}
}