• March 26, 2025

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

FeatureCollectionStream
TypeData StructureData Processing Pipeline
Data StorageStores elements in memoryDoes not store elements
ModificationSupports add, remove, updateImmutable (cannot modify original data)
ProcessingIterative (loop-based)Functional (map, filter, reduce)
ExecutionProcesses elements one by oneSupports lazy and parallel execution
ReusabilityCan be reused multiple timesCannot 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, while Collection is for data storage and manipulation.

Would you like a comparison between Stream vs ParallelStream? 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *