Difference between using a Stream or Collection

Asked

Viewed 124 times

1

I see that the use of stream Java 8 is recommended for large amount of data. If the data set is small, there is some gain from using only Collection?

1 answer

2


The stream was not created to improve something according to the amount given. It exists to give more expressiveness, and supposedly legibility. So your code should be clearer, and simple in dicado what you want without worrying about how to do it. It’s a way to make language more declarative and less imperative.

If you’re thinking about performance then the stream is bad, especially with large volumes, because with small all performance is acceptable.

  • Let’s say I don’t want to process all this data as a Collection

  • Again, the stream was not created for this, you can do the same without stream, will only be more verbose and risk more, no matter the amount of data, or whether it will be partial or not. The stream was not created to process something partial, this is something of some specific algorithms that have already created in the stream, several of them is even possible to partially execute.

  • Maniero which is the sort algorithm used in streams?

  • @ayowoleagbedejobi I don’t know what you’re talking about, it’s without context.

  • @Maniero In this case I use the sort method called 'Sorted' in Stream, only I don’t know the sort algorithm that is used when invoking this method and if it is an efficient algorithm.

  • If this doubt of mine escapes the scope of this question let me know that I create another question.

  • @ayowoleagbedejobi Ok, you use a method among them that makes classification of the collection. I can not say what it is, because it should be an implementation detail, but the subject here is not about this, can ask a question and who knows can answer.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.