Friday, November 15, 2019

Collection Vs Stream API In Java 8

Q- What is the difference between Collection Vs Stream in java 8?
Main differences between Collection and Stream is that collection are used to store date but stream does not store data. Stream is used to perform operations on that data. An operation on a stream does not modify its source, but simply produces a result.

With Collection API we can store a finite number of elements in a data structure. But with Stream API, we can handle streams of data that can contain infinite number of elements.

Collections are used to store the data in a data structure like List, Set or Map. But, streams is used to perform operation on complex data like filter, map, sorted, concat etc on data such as arrays, collections or I/O resources.

 External Iteration Vs Internal Iteration: The specialty of  Stream is that we need not to worry about iteration while using stream. Streams perform iteration internally we just need to mention the operations to be performed on a source.But on collection we have to do the iteration externally over collections using loops.

Traversal: Collections can be traversed multiple times. But Streams are traversable only once. Because If we traverse the stream once, that means consumed. To traverse it again, we have to get new stream from the source again.

Eager Construction Vs Lazy Construction: Collections are constructed  eagerly i.e all the elements are computed at the beginning itself. But, streams are lazily constructed i.e intermediate operations are not evaluated until terminal operation is invoked.


Related Tutorial

No comments:

Post a Comment