Merge pull request #15727 from pivovarit/bump-parallel-collectors
Refresh parallel-collectors examples
This commit is contained in:
commit
74c62ec2f6
|
@ -49,11 +49,11 @@
|
|||
|
||||
<properties>
|
||||
<jool.version>0.9.12</jool.version>
|
||||
<parallel-collectors.version>1.1.0</parallel-collectors.version>
|
||||
<parallel-collectors.version>2.6.0</parallel-collectors.version>
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||
<streamex.version>0.8.1</streamex.version>
|
||||
<protonpack.version>1.15</protonpack.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.parallel_collectors;
|
||||
|
||||
import com.pivovarit.collectors.ParallelCollectors;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -12,13 +13,15 @@ import java.util.concurrent.ExecutorService;
|
|||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallel;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelOrdered;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToCollection;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToList;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToMap;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToOrderedStream;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToStream;
|
||||
import static java.util.concurrent.CompletableFuture.completedFuture;
|
||||
import static java.util.stream.Collectors.toCollection;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ParallelCollectorsUnitTest {
|
||||
|
||||
|
@ -28,21 +31,21 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<String> results = ids.parallelStream()
|
||||
.map(i -> fetchById(i))
|
||||
.collect(Collectors.toList());
|
||||
.collect(toList());
|
||||
|
||||
System.out.println(results);
|
||||
assertThat(results).containsExactly("user-1", "user-2", "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldProcessInParallelWithParallelCollectors() {
|
||||
public void shouldCollectInParallel() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
CompletableFuture<List<String>> results = ids.stream()
|
||||
.collect(parallelToList(ParallelCollectorsUnitTest::fetchById, executor, 4));
|
||||
CompletableFuture<Stream<String>> results = ids.stream()
|
||||
.collect(parallel(ParallelCollectorsUnitTest::fetchById, executor, 4));
|
||||
|
||||
System.out.println(results.join());
|
||||
assertThat(results.join()).containsExactly("user-1", "user-2", "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -51,11 +54,10 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.stream()
|
||||
.collect(parallelToList(ParallelCollectorsUnitTest::fetchById, executor, 4))
|
||||
.join();
|
||||
CompletableFuture<List<String>> results = ids.stream()
|
||||
.collect(parallel(ParallelCollectorsUnitTest::fetchById, toList(), executor, 4));
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
assertThat(results.join()).containsExactly("user-1", "user-2", "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -64,11 +66,11 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.stream()
|
||||
.collect(parallelToCollection(i -> fetchById(i), LinkedList::new, executor, 4))
|
||||
.join();
|
||||
CompletableFuture<List<String>> results = ids.stream()
|
||||
.collect(parallel(i -> fetchById(i), toCollection(LinkedList::new), executor, 4));
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
assertThat(results.join())
|
||||
.containsExactly("user-1", "user-2", "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -77,12 +79,13 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, List<String>> results = ids.stream()
|
||||
.collect(parallelToStream(i -> fetchById(i), executor, 4))
|
||||
.thenApply(stream -> stream.collect(Collectors.groupingBy(i -> i.length())))
|
||||
.join();
|
||||
CompletableFuture<Map<Integer, List<String>>> results = ids.stream()
|
||||
.collect(parallel(i -> fetchById(i), executor, 4))
|
||||
.thenApply(stream -> stream.collect(Collectors.groupingBy(String::length)));
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
assertThat(results.join())
|
||||
.hasSize(1)
|
||||
.containsEntry(6, Arrays.asList("user-1", "user-2", "user-3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -91,9 +94,10 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
ids.stream()
|
||||
.collect(parallel(ParallelCollectorsUnitTest::fetchByIdWithRandomDelay, executor, 4))
|
||||
.forEach(System.out::println);
|
||||
Stream<String> result = ids.stream()
|
||||
.collect(parallelToStream(ParallelCollectorsUnitTest::fetchByIdWithRandomDelay, executor, 4));
|
||||
|
||||
assertThat(result).contains("user-1", "user-2", "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -102,9 +106,10 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
ids.stream()
|
||||
.collect(parallelOrdered(ParallelCollectorsUnitTest::fetchByIdWithRandomDelay, executor, 4))
|
||||
.forEach(System.out::println);
|
||||
Stream<String> result = ids.stream()
|
||||
.collect(parallelToOrderedStream(ParallelCollectorsUnitTest::fetchByIdWithRandomDelay, executor, 4));
|
||||
|
||||
assertThat(result).containsExactly("user-1", "user-2", "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -113,24 +118,14 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, ParallelCollectorsUnitTest::fetchById, executor, 4))
|
||||
.join();
|
||||
CompletableFuture<Map<Integer, String>> results = ids.stream()
|
||||
.collect(parallel(i -> i, Collectors.toMap(i -> i, ParallelCollectorsUnitTest::fetchById), executor, 4));
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToTreeMap() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, ParallelCollectorsUnitTest::fetchById, TreeMap::new, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
assertThat(results.join())
|
||||
.hasSize(3)
|
||||
.containsEntry(1, "user-1")
|
||||
.containsEntry(2, "user-2")
|
||||
.containsEntry(3, "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -139,11 +134,24 @@ public class ParallelCollectorsUnitTest {
|
|||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, ParallelCollectorsUnitTest::fetchById, TreeMap::new, (s1, s2) -> s1, executor, 4))
|
||||
.join();
|
||||
CompletableFuture<Map<Integer, String>> results = ids.stream()
|
||||
.collect(parallel(i -> i, Collectors.toMap(i -> i, ParallelCollectorsUnitTest::fetchById, (u1, u2) -> u1, TreeMap::new), executor, 4));
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
assertThat(results.join())
|
||||
.hasSize(3)
|
||||
.containsEntry(1, "user-1")
|
||||
.containsEntry(2, "user-2")
|
||||
.containsEntry(3, "user-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectListOfFutures() {
|
||||
List<CompletableFuture<Integer>> futures = Arrays.asList(completedFuture(1), completedFuture(2), completedFuture(3));
|
||||
|
||||
CompletableFuture<List<Integer>> result = futures.stream()
|
||||
.collect(ParallelCollectors.toFuture());
|
||||
|
||||
assertThat(result.join()).containsExactly(1, 2, 3);
|
||||
}
|
||||
|
||||
private static String fetchById(int id) {
|
||||
|
|
Loading…
Reference in New Issue