Pergunta

I started studying functional programming with JavaScript. After this, I started to study it with Java 8 (streams, lambdas and method reference) and I realised that I tend to use streams as much as possible, avoiding collections.

I heard that there is no sense to use streams when there are no events in the context. So, it makes me wonder:

What is the original problem that is intended to be resolved with streams?

I think that if I have the answer to this question, I could choose adequately when to use one or another.

Foi útil?

Solução

Streams and collections are not alternatives, they serve completely different purposes.

java.util.Collection is an interface for representing a "collection of things", its primary purpose is to keep things, like an array in JavaScript.

java.util.Stream is an interface for processing things in multiple stages.

In most cases you will actually have both, a Collection containing things, and then you call .stream() on it to do some processing.

My guess is that the alternative to streams you actually mean is the "enhanced for loop":

for(String s: collectionInstance){
    doSomethingWith(s);
}

which is an alternative to

collectionInstance.stream().forEach(this::doSomethingWith);

And here the advantages of streams is:

  • the code can be cleaner by separating multiple operations and reducing intermediate variables. It's also a statement, so you can assign or return the collected result directly rather than having to create a target collection beforehand.
  • it can reduce memory usage by making it easier to avoid keeping full collections with intermediate results in memory.

But streams can also be used directly, not based on collections. You don't need to keep all elements in memory at all then. You can even have infinite streams that are generated on the fly which you stop processing at some point; try doing that with a collection!

The advantage of using the for loop is mainly that it's easier to debug, since there is less magic happening. It's also still more familiar for most developers.

Outras dicas

I use collectionInstance.stream().forEach() for processing big data: the code can be executed in parallel, distributed on a cluster

See wikipedia MapReduce for details

Licenciado em: CC-BY-SA com atribuição
scroll top