2

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.

11

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.

  • Good answer. I've seen a trend of (IMO) an overuse of streams of for loops. There's a belief that that looping with a for loop is somehow slower than forEach which is, by itself, completely wrong. I'm a little more confident in this as I see people creating bugs in the process of trying to use streams when a loop is more natural. One minor nit: the Collection interface doesn't require keeping things in memory. All you need to know is the number of items. And if you don't, you can always return an Iterator. – JimmyJames Mar 12 at 14:45
  • Good answer! It helped me a lot! – Bruno Carneiro Mar 12 at 23:51
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.