Skip to content

Latest commit

 

History

History
174 lines (100 loc) · 4.73 KB

File metadata and controls

174 lines (100 loc) · 4.73 KB

Java Collections vs. Streams Streamlined

🔹 1. Basic Definition

Collections

A Collection is a data structure (e.g., List, Set, Map) that stores elements in memory. It represents a container of objects that you can add to, remove from, or iterate over.

Example:

 // A Collection stores data.
  List names = new ArrayList<>();
  names.add("Alice");
  names.add("Bob");   

Streams

A Stream is a sequence of elements derived from a source like a collection, array, or I/O channel. It is not a data structure; it doesn’t store elements itself. It’s an abstraction for processing data in a declarative, functional style.

Example:

// A Stream processes data from a source.
  List names = Arrays.asList("Alice", "Bob", "Charlie");
   names.stream().filter(n -> n.startsWith("A"))       
   .forEach(System.out::println); 
   // Output: Alice   

🔹 2. Key Differences

FeatureCollectionsStreams

Nature: Data structure holding elements in memory.Abstraction to process data (pipeline).

Storage: Stores elements (can add/remove).Doesn’t store elements; it processes them from a source.

Traversal: Can be traversed multiple times.Traversed only once.

After a terminal operation, the stream is consumed.

Modification: You can modify elements (add, remove, update).You cannot modify the source; only transform elements into a new form.

EagernessEager: Operations are executed immediately.

Lazy: Intermediate operations are not executed until a terminal one.

IterationExternal: Explicit iteration (for, while, Iterator).

Internal: Iteration is handled for you (functional style).

Parallelism: Manual parallelization (threads, executors).Built-in support via .parallelStream() or .parallel().

Reusability: A collection can be reused many times.A stream can be consumed only once.

API StyleImperative: You write how to do something.

Declarative: You describe what you want to achieve.Export to Sheets

🔹 3. When to Use Collections

✅ Use Collections when:

  • You need to store and manage data in memory (CRUD operations).

  • You want to add, remove, or update elements dynamically.

  • You need random access to elements by index (e.g., list.get(5)).

Example Applications:

  • Maintaining a list of currently online users: Set onlineUsers.

  • Caching frequently used data in memory.

  • Implementing custom data structures like queues, stacks, etc.

🔹 4. When to Use Streams

✅ Use Streams when:

  • You need to process or transform data from a source.

  • You want to apply functional-style operations like map, filter, reduce.

  • You are performing bulk data operations (aggregation, grouping, statistics).

  • You want easy parallel processing without managing threads manually.

Example Applications:

  • Filtering orders greater than $1000:
orders.stream().filter(o -> o.getAmount() > 1000).forEach(System.out::println);   

  • Summing a list of numbers:
   int total = numbers.stream() 
                .mapToInt(Integer::intValue)
                .sum();   

  • Collecting results into another collection:
   List upperCaseNames = names.stream().map(String::toUpperCase)                                     .toList();   

🔹 5. Complementary Usage

Most real-world Java applications use both together. They are not competitors; they are partners.

  1. A Collection stores the data.

  2. A Stream is used to process the data from the collection.

Example:

   // 1. A Collection (List) holds the employee data.
     List employees = getEmployeesFromDatabase();  
     
     // 2. A Stream is used to process the data.
       double avgSalaryIT = employees.stream()      .filter(e -> e.getDepartment().equals("IT")) // Find IT employees 
            .mapToDouble(Employee::getSalary) // Get their salaries      
            .average()// Calculate the average     
            .orElse(0.0); // Default if no IT employees   

🔹 6. Which is Better?

👉 This isn't about which is "better," but which is right for the job.

Collections are about DATA.Streams are about OPERATIONS on data.

  • If you need to store, update, and retrieve elements, use a Collection.

  • If you need to process and transform data, use a Stream.

In Practice:

  • Use Collections as your data layer (for in-memory storage).

  • Use Streams as your processing layer (for functional transformations and queries).