@@ -42,8 +42,10 @@ class ParallelOperationCollectionsUnitTest {
42
42
}
43
43
44
44
private val columnScheme = " %-25s %-45s %-40s"
45
+
46
+ private fun Person.setAdultStatus (){
47
+ this .isAdult = this .age >= 18
45
48
46
- private fun Person.printFormattedInfo () {
47
49
println (
48
50
columnScheme.format(
49
51
SimpleDateFormat (" yyyy-MM-dd:HH:mm:ss:SSS" ).format(Date .from(Instant .now())),
@@ -68,8 +70,7 @@ class ParallelOperationCollectionsUnitTest {
68
70
69
71
val filteredPeople = people.map { person ->
70
72
async {
71
- person.isAdult = person.age >= 18
72
- person.printFormattedInfo()
73
+ person.setAdultStatus()
73
74
person
74
75
}
75
76
}.awaitAll().filter { it.age > 15 }.sortedBy { it.age }
@@ -88,8 +89,7 @@ class ParallelOperationCollectionsUnitTest {
88
89
val filteredPeople = people.asFlow().flatMapMerge { person ->
89
90
flow {
90
91
emit(async {
91
- person.isAdult = person.age >= 18
92
- person.printFormattedInfo()
92
+ person.setAdultStatus()
93
93
person
94
94
}.await())
95
95
}
@@ -107,8 +107,7 @@ class ParallelOperationCollectionsUnitTest {
107
107
108
108
val observable = Observable .fromIterable(people).flatMap({
109
109
Observable .just(it).subscribeOn(Schedulers .computation()).doOnNext { person ->
110
- person.isAdult = person.age >= 18
111
- person.printFormattedInfo()
110
+ person.setAdultStatus()
112
111
}
113
112
}, people.size) // Uses maxConcurrency for the number of elements
114
113
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
@@ -125,8 +124,7 @@ class ParallelOperationCollectionsUnitTest {
125
124
126
125
val observable = people.toObservable().flatMap({
127
126
Observable .just(it).subscribeOn(Schedulers .computation()).doOnNext { person ->
128
- person.isAdult = person.age >= 18
129
- person.printFormattedInfo()
127
+ person.setAdultStatus()
130
128
}
131
129
}, people.size) // Uses maxConcurrency for the number of elements
132
130
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
@@ -143,8 +141,7 @@ class ParallelOperationCollectionsUnitTest {
143
141
144
142
val observable =
145
143
people.toObservable().subscribeOn(Schedulers .io()).flatMap { Observable .just(it) }.doOnNext { person ->
146
- person.isAdult = person.age >= 18
147
- person.printFormattedInfo()
144
+ person.setAdultStatus()
148
145
}.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
149
146
150
147
startTime.printTotalTime()
@@ -158,8 +155,8 @@ class ParallelOperationCollectionsUnitTest {
158
155
val startTime = Instant .now()
159
156
160
157
val filteredPeople = people.parallelStream().map { person ->
161
- person.isAdult = person.age >= 18
162
- person.printFormattedInfo ()
158
+
159
+ person.setAdultStatus ()
163
160
person
164
161
}.filter { it.age > 15 }.sorted { p1, p2 -> p1.age.compareTo(p2.age) }.collect(Collectors .toList())
165
162
@@ -176,8 +173,7 @@ class ParallelOperationCollectionsUnitTest {
176
173
val executor = Executors .newFixedThreadPool(people.size)
177
174
val futures = people.map { person ->
178
175
executor.submit(Callable {
179
- person.isAdult = person.age >= 18
180
- person.printFormattedInfo()
176
+ person.setAdultStatus()
181
177
person
182
178
})
183
179
}.map { it.get() }.filter { it.age > 15 }.sortedBy { it.age }
0 commit comments