Giter Club home page Giter Club logo

Comments (7)

pivovarit avatar pivovarit commented on May 27, 2024

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.

pivovarit avatar pivovarit commented on May 27, 2024

Let me know if you still have questions, closing for now

from parallel-collectors.

dfit99 avatar dfit99 commented on May 27, 2024

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.

pivovarit avatar pivovarit commented on May 27, 2024

No, just the part that lands in the collector

from parallel-collectors.

dfit99 avatar dfit99 commented on May 27, 2024

Hi @pivovarit

Thank you for the quick response -- do you have any advice on how I can go about parallelizing filtering?

from parallel-collectors.

pivovarit avatar pivovarit commented on May 27, 2024

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.

tylorJ905 avatar tylorJ905 commented on May 27, 2024

from parallel-collectors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.