Return to site

What’s the difference between a Collection and a Stream in Java?

· java,interview,video

Both are used to work with groups of objects, but they serve very different purposes:
📋 Collection (Java Collections Framework)
➡️ Stores data in memory (e.g., List, Set, Map)
➡️ Focuses on data storage and retrieval
➡️ Supports iteration (for-each, iterator)
➡️ Example:
List〈String〉names = List.of("Alice", "Bob");


🌊 Stream (Java 8 Streams API)
➡️ Does not store data, works on data from a collection or other source
➡️ Focuses on data processing (filtering, mapping, reducing)
➡️ Can be sequential or parallel
➡️ Example:
names.stream().filter(n -〉 n.startsWith("A")).forEach(System.out::println);


🧠 Key difference: