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.