Collection vs Stream: What is Difference?
In Java, Collection and Stream are both important concepts, but they serve different purposes:
Collection
is a data structure that stores and manages a group of objects.Stream
is an abstraction for performing operations on a collection, such as filtering, mapping, and reducing data.
1. What is Collection?
A Collection is a container that holds multiple elements. It provides methods to add, remove, search, and iterate over elements.
Features of Collection Interface:
✔ Stores data in memory (like List
, Set
, Queue
).
✔ Allows modifications (add, remove, update).
✔ Supports iterative processing (using loops, iterators).
✔ Implements Iterable, so it supports forEach()
.
Collection Hierarchy
plaintextCopy codeCollection (Interface)
├── List (Interface) → ArrayList, LinkedList, Vector
├── Set (Interface) → HashSet, TreeSet, LinkedHashSet
└── Queue (Interface) → PriorityQueue, Deque
Example Using Collection
import java.util.*;
public class CollectionExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
for (Integer num : numbers) {
System.out.println(num);
}
}
}
🔹 Here, Collection
stores data and is accessed using a for
loop.
2. What is Stream?
A Stream is a sequence of elements that supports functional-style operations on data. It is not a data structure but a pipeline for processing elements.
Features of Stream API:
✔ Works with Collections, Arrays, or I/O sources.
✔ Supports functional programming (map()
, filter()
, reduce()
).
✔ Does not store data, but processes elements on-demand.
✔ Cannot modify the original data (immutable).
✔ Supports parallel execution (parallelStream()
).
Example Using Stream
import java.util.*;
import java.util.stream.*;
public class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50);
// Using Stream to filter and print numbers > 20
numbers.stream()
.filter(num -> num > 20)
.forEach(System.out::println);
}
}
🔹 Here, Stream
filters the list without modifying the original collection.
3. Key Differences Between Collection and Stream
Feature | Collection | Stream |
---|---|---|
Type | Data Structure | Data Processing Pipeline |
Data Storage | Stores elements in memory | Does not store elements |
Modification | Supports add, remove, update | Immutable (cannot modify original data) |
Processing | Iterative (loop-based) | Functional (map, filter, reduce) |
Execution | Processes elements one by one | Supports lazy and parallel execution |
Reusability | Can be reused multiple times | Cannot be reused (must create a new stream) |
4. When to Use Collection vs Stream?
- Use
Collection
when you need to store and modify data (e.g.,ArrayList
,HashSet
). - Use
Stream
when you need to process data efficiently (e.g., filtering, mapping).
5. Conclusion
Collection
is a data structure used for storing elements.Stream
is a data processing API that works on a collection without modifying it.Stream
is best for functional operations, whileCollection
is for data storage and manipulation.
Would you like a comparison between Stream vs ParallelStream? 🚀