What is the difference between Java Stream and Collection api ?
Java Stream was
introduced by Oracle in Java 8 as a new and much efficient way to iterate over
collection of data which may use internal parallelism as well without user
actually using any thread declarations.
There are some basic differences
between old Collection api and new Streams, which are outlined below:
1. Usage:
Collection API is used for storing data in different kinds of data structures.
Stream API is used for computation of data on a large set of Objects. In Other
words, Streams are used for manipulating the data for various purposes like
converting it into some other format or converting one Collection to other
Collection etc. while Collection itself is used to store the data.
2. Finite:
With Collection API we can store a finite number of elements in a data
structure. With Stream API, we can handle streams of data that can contain
infinite number of elements. Like,
Stream can used for all the odd prime numbers but you cannot use a
Collection to do the same as if you try to do that it would be an infinite loop
which would never complete, it will keep on computing new odd primes and adding
to the collection and never finish.
3. Eager
vs. Lazy: Collection API constructs objects in an eager
manner. Stream API creates objects in a lazy manner. In Collection all the
elements needs to be stored in the data structure before the user can use it
but same is not true for Streams. A real life analogy would be , suppose you
want to watch a movie, Collection would be something like a DVD which has all
the movie bytes stored in there before you start using it while Stream would be
like Online streaming the movie where bytes are transferred on need basis and
continuously.
4. Multiple
consumption: Most of the Collection APIs support iteration and
consumption of elements multiple times. With Stream API we can consume or
iterate elements only once. Once you are done with your operation on the stream
it is called to be exhausted and cannot be used further. For new operation you
have to create new stream again.
5. Parallel
processing: With
Collection api if you want to go with parallel processing you have to write
your own explicit code using java multithreading concepts in most of the cases
while in case of Stream you can just use ParallelStream in place of Stream
and parallelism would be handled internally by java.
6. Internal vs External iteration:
This is also one of the important differences between Collection and Stream. In
Collection, iteration is done through iterator which the developer needs to
write by himself in code which is called External iteration while in case of
Stream developer does not need to write any Iterator, its java which manages
how to iterate based on best possible approach.
Above mentioned differences are basic
and easily understandable ones. Please mention in comments other difference and
I would include that in the list.
What Is The Difference Between Java Stream And Collection Api ? - Crack That Code.. >>>>> Download Now
ReplyDelete>>>>> Download Full
What Is The Difference Between Java Stream And Collection Api ? - Crack That Code.. >>>>> Download LINK
>>>>> Download Now
What Is The Difference Between Java Stream And Collection Api ? - Crack That Code.. >>>>> Download Full
>>>>> Download LINK