Comments (7)
Hi @davidmav!
Thanks! When I started writing it... I was surprised that no one did that before!
So, Parallel Collectors are just... a set of collectors for existing Stream API. There's no reflection, bytecode injection, or any other black magic - just using Stream API public interfaces.
This makes it fully compatible with any construct of Stream API. Technically, you should be able to replace any Collectors.toList()
with any of these, and that should just work :)
So, to answer directly your question, yes you can. Check the following copy-paste'able snippet:
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
List<List<Integer>> lists = asList(asList(1), asList(2, 3), asList(4, 5, 6));
List<String> join = lists.stream()
.flatMap(list -> list.stream())
.filter(i -> i % 2 == 0)
.collect(ParallelCollectors.parallelToList(i -> foo(i), executor))
.join();
System.out.println(join);
executor.shutdownNow();
}
private static String foo(Integer i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return i.toString();
}
Does that help?
from parallel-collectors.
Let me know if you still have questions, closing for now
from parallel-collectors.
Hi @pivovarit I wanted to follow up on this question, in your example does .filter(i -> i % 2 == 0)
get parallelized as well?
from parallel-collectors.
No, just the part that lands in the collector
from parallel-collectors.
Hi @pivovarit
Thank you for the quick response -- do you have any advice on how I can go about parallelizing filtering?
from parallel-collectors.
For now, you could try a trick in which you do the filtering inside a map() of a parallel collector so that you end up with Stream<Boolean>
or some form of a pair <Stream<Pair<Boolean, T>>
and then you can simply do the cheap filtering sequentially
from parallel-collectors.
from parallel-collectors.
Related Issues (20)
- Short-circuit when exception gets thrown HOT 2
- Should unlimited parallelism level be allowed? HOT 1
- Remove repetition from tests HOT 2
- Improve ThrottlingParallelCollector implementation
- Please avoid suggesting parallel N+1 database interaction patterns HOT 4
- Extract common logic to ParallelDispatcher
- Implement ParallelCollectors.parallelToListOrdered accepting user-provided List implementations
- Remove deprecated methods
- Remove UnboundedDispatcher
- Remove *Ordered API methods and make all of them ordered by default
- Introduce API methods that allow reusing native Java Collectors HOT 2
- Provide a possibility to opt-in into Spliterator-based splitting instead of processing each element separately HOT 1
- Enable batching scheduling strategy
- Optimize parallelism == 1 edge cases
- Implement ParallelCollectors.toCombinedFuture
- Inconsistent behavior when parallel=1 HOT 9
- Deprecate API methods with implicit parallelism resolution
- Support spring cloud sleuth HOT 4
- Make it possible to configure the dispatcher instance in the Dispatcher HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parallel-collectors.