Skip to content

Commit 25863c4

Browse files
hanggatheangrydev
andauthored
Update core-kotlin-modules/core-kotlin-collections-6/src/test/kotlin/com/baeldung/parallelOperationsCollections/ParallelOperationCollectionsUnitTest.kt
agree.. Co-authored-by: Liam Williams <[email protected]>
1 parent 53bac61 commit 25863c4

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

core-kotlin-modules/core-kotlin-collections-6/src/test/kotlin/com/baeldung/parallelOperationsCollections/ParallelOperationCollectionsUnitTest.kt

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ class ParallelOperationCollectionsUnitTest {
3535

3636
private fun List<Person>.assertResultsTrue() {
3737
assertThat(this).containsExactly(
38-
Person("Bob", 16, false),
39-
Person("Alice", 30, true),
40-
Person("Charlie", 40, true),
41-
Person("Ahmad", 42, true)
42-
)
38+
Person("Bob", 16, false),
39+
Person("Alice", 30, true),
40+
Person("Charlie", 40, true),
41+
Person("Ahmad", 42, true)
42+
)
4343
}
4444

4545
private fun String.printAsHeader() {
@@ -63,12 +63,15 @@ class ParallelOperationCollectionsUnitTest {
6363
"Using Coroutines".printAsHeader()
6464
val startTime = Instant.now()
6565

66-
val filteredPeople = people.map { person ->
66+
val filteredPeople = people
67+
.map { person ->
6768
async {
6869
person.setAdult()
6970
person
7071
}
71-
}.awaitAll().filter { it.age > 15 }.sortedBy { it.age }
72+
}.awaitAll()
73+
.filter { it.age > 15 }
74+
.sortedBy { it.age }
7275

7376
startTime.printTotalTime()
7477

@@ -81,7 +84,8 @@ class ParallelOperationCollectionsUnitTest {
8184
"Using Kotlin Flow".printAsHeader()
8285
val startTime = Instant.now()
8386

84-
val filteredPeople = people.asFlow().flatMapMerge { person ->
87+
val filteredPeople = people.asFlow()
88+
.flatMapMerge { person ->
8589
flow {
8690
emit(
8791
async {
@@ -90,7 +94,9 @@ class ParallelOperationCollectionsUnitTest {
9094
}.await()
9195
)
9296
}
93-
}.filter { it.age > 15 }.toList().sortedBy { it.age }
97+
}
98+
.filter { it.age > 15 }.toList()
99+
.sortedBy { it.age }
94100

95101
startTime.printTotalTime()
96102

@@ -102,7 +108,8 @@ class ParallelOperationCollectionsUnitTest {
102108
"Using RxJava".printAsHeader()
103109
val startTime = Instant.now()
104110

105-
val observable = Observable.fromIterable(people).flatMap(
111+
val observable = Observable.fromIterable(people)
112+
.flatMap(
106113
{
107114
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
108115
person.setAdult()
@@ -124,7 +131,8 @@ class ParallelOperationCollectionsUnitTest {
124131
"Using RxKotlin".printAsHeader()
125132
val startTime = Instant.now()
126133

127-
val observable = people.toObservable().flatMap(
134+
val observable = people.toObservable()
135+
.flatMap(
128136
{
129137
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
130138
person.setAdult()
@@ -162,7 +170,8 @@ class ParallelOperationCollectionsUnitTest {
162170
"Using Stream API".printAsHeader()
163171
val startTime = Instant.now()
164172

165-
val filteredPeople = people.parallelStream().map { person ->
173+
val filteredPeople = people.parallelStream()
174+
.map { person ->
166175
person.setAdult()
167176
person
168177
}.filter { it.age > 15 }
@@ -180,12 +189,15 @@ class ParallelOperationCollectionsUnitTest {
180189
val startTime = Instant.now()
181190

182191
val executor = Executors.newFixedThreadPool(people.size)
183-
val futures = people.map { person ->
192+
val futures = people
193+
.map { person ->
184194
executor.submit(Callable {
185195
person.setAdult()
186196
person
187-
})
188-
}.map { it.get() }.filter { it.age > 15 }.sortedBy { it.age }
197+
}).get()
198+
}
199+
.filter { it.age > 15 }
200+
.sortedBy { it.age }
189201

190202
executor.shutdown()
191203

0 commit comments

Comments
 (0)