A Collection is an in-memory data structure, which holds all the values that the data structure currently has. Every element in the Collection has to be computed before we add it to the Collection. Operations such as searching, sorting, insertion, manipulation, and deletion can be performed on a Collection. It provides many interfaces like (Set, List, Queue, Deque) and Classes like (ArrayList, Vector, LinkedList, PriorityQueue, HashSet).
A Stream is a fixed data structure, in which the elements are computed on demand. The Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods that can be pipelined to produce the desired result.
The features of Java stream are:
- It takes input from the Collections, Arrays or I/O channels and is not a data structure.
- Streams only provide the result as per the pipelined methods and don’t change the original data structure.
|It doesn’t store data, it operates on the source data structure i.e collection.||It stores/holds all the data that the data structure currently has in a particular data structure like Set, List or Map,|
|They use functional interfaces like lambda which makes it a good fit for programming language.||They don’t use functional interfaces.|
|Java Streams are consumable i.e; to traverse the stream, it needs to be created every time.||They are non-consumable i.e; can be traversable multiple times without creating it again.|
|Java streams support both sequential and parallel processing.||It supports parallel processing and parallel processing can be very helpful in achieving high performance.|
|All the Java stream API interfaces and classes are in java.util.stream package.||Specific classes for primitive types such as IntStream, LongStream, and DoubleStream are used in collections since primitive data types such as int, long in the collections using auto-boxing and these operations could take a lot of time.|
|Streams are not modifiable i.e one can’t add or remove elements from streams.||These are modifiable i.e one can easily add to or remove elements from collections.|
|Streams are iterated internally by just mentioning the operations.||Collections are iterated externally using loops.|
Example of Collections
Apple Google Microsoft
Example of Streams
Apple Google Microsoft
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.