0

I have some java code that I want to chain together using Java 8's stream. Here's the code in the current state:

public class FooOperations {
    public List<FooB> getFooB() {
        List<FooA> fooAs = fooAOperation.getFooA();
        List<FooB> fooBs = fooBOperation.getFooB(fooAs);
        return fooBs;
    }
}

public class FooAOperation {
    public List<FooA> getFooA() {
        return foos.parallelStream().map(build()).filter(Objects::nonNull).collect(Collectors.toList());
    }
}

public class FooBOperation {
    public List<FooB> getFooB(List<FooA> fooAs) {
        return fooAs.parallelStream()
                    .map(fooA -> generateFooB(fooA))
                    .collect(Collectors.toList());
    }
}

But I want to chain these operations into one line in the FooOperations class. Something like this:

return fooAOperation.getFooA().map(fooA -> FooBOperation::getFooB(fooA)).collect(Collectors.toList());

Which would require the FooAOperation class to return a stream rather than a list.

My objective is to chain these two processes together so that the FooBOperation doesn't have to wait for the full list of FooA objects to be populated before it can start processing; I want to make it so that it can start processing the objects individually as soon as they're ready. Kind of a like a message queue.

What is the best way to go about this?

Holger
  • 285,553
  • 42
  • 434
  • 765
Richard
  • 5,840
  • 36
  • 123
  • 208

0 Answers0