Close Menu
Şevket Ayaksız
    What's Hot

    Java Array Copies: A New Array Does Not Always Mean Independent Objects

    November 3, 2026

    Oracle COUNT: Define the Metric Before Choosing the Expression

    November 3, 2026

    Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result

    November 3, 2026
    • software
    • Gadgets
    Şevket AyaksızŞevket Ayaksız
    • Home
    • Technology

      Apple: iPhone 17 and Older Models Hit With Unexpected $100 Price Increase

      September 10, 2026

      Apple Watch Intelligence Could Be a Major Accessibility Breakthrough

      September 10, 2026

      Apple May Skip the Base iPhone 18 This Year

      September 10, 2026

      FCC changes robot vacuum rules, potentially affecting future models

      August 8, 2026

      Samsung’s new 2TB 990 SSD drops to its lowest price yet

      August 6, 2026
    • Adobe

      Illustrator Linked Images: Build a Handoff That Opens Correctly on Another Computer

      November 3, 2026

      Lightroom Classic Export Settings: Pixel Dimensions and Print Resolution Explained

      November 2, 2026

      InDesign Text Flow: Use Breaks for Destinations and Paragraph Rules for Relationships

      October 31, 2026

      Premiere Audio Automation: Choose Between Clip and Track Changes

      October 31, 2026

      Photoshop Layer Masks: Fix the Visibility Without Repainting the Photograph

      October 30, 2026
    • Microsoft

      Excel SUBTOTAL: Make Your Total Match the Rows You Mean to Include

      November 3, 2026

      How to Make a Smaller PowerPoint Delivery Copy Without Losing Your Master

      November 2, 2026

      How to Keep Leading Zeros and Long Identifiers Safe in Excel

      November 2, 2026

      Excel XLOOKUP: A Matching Result Does Not Prove a Unique Record

      October 31, 2026

      Word Headers and Footers: Understand the Section Before Editing the Page

      October 30, 2026
    • java

      Java Array Copies: A New Array Does Not Always Mean Independent Objects

      November 3, 2026

      Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result

      November 3, 2026

      Java String Replacement: When to Use Literal Text and When to Use Regex

      November 2, 2026

      Java Periodic Tasks: Choose the Schedule and the Failure Policy Together

      October 31, 2026

      Java Text Files: Choose Between Whole-File and Line-Based Reading

      October 30, 2026
    • Oracle

      Oracle COUNT: Define the Metric Before Choosing the Expression

      November 3, 2026

      Oracle EXISTS: Find Matching Parent Records Without Multiplying Rows

      November 2, 2026

      Oracle LISTAGG: Make Long-List Truncation Visible to the Reader

      October 31, 2026

      Oracle UNION and UNION ALL: Decide Whether Repeated Rows Carry Meaning

      October 30, 2026

      Oracle Sequences: A Missing Number Does Not Prove a Missing Row

      October 30, 2026
    Şevket Ayaksız
    Anasayfa » Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result
    java

    Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result

    By sevketayaksizNovember 3, 2026No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result
    Share
    Facebook Twitter LinkedIn Pinterest Email

    A Java stream variable can look like a reusable collection. That similarity becomes a problem when code calculates one result and then tries to run another terminal operation on the same stream.

    The stream represents a processing pipeline, not a stored container of results. When an application needs several calculations, it should deliberately choose between creating fresh streams, storing an intermediate result, or combining the work.

    Understand the lifecycle

    Java’s Stream documentation states that a stream should be operated on only once. Reuse may be detected and produce an IllegalStateException, but applications must not depend on every invalid reuse being detected.

    Consider this example:

    Stream<String> names = people.stream()
        .filter(name -> name.startsWith("A"));
    
    long count = names.count();
    List<String> selected = names.toList();

    The second terminal operation attempts to reuse the pipeline.

    The problem is not fixed by renaming the variable or assigning it to another variable. Both references still describe the same stream.

    Create a fresh stream when the source is reusable

    For an ordinary collection that remains available, create a new pipeline for each independent calculation:

    long count = people.stream()
        .filter(name -> name.startsWith("A"))
        .count();
    
    List<String> selected = people.stream()
        .filter(name -> name.startsWith("A"))
        .toList();

    This makes each traversal explicit. However, it also repeats the filtering work.

    If both results need to describe exactly the same selected values, another design is to materialize that selection once:

    List<String> selected = people.stream()
        .filter(name -> name.startsWith("A"))
        .toList();
    
    long count = selected.size();

    The right choice depends on the size of the data and the results the application actually needs. Avoid storing a large intermediate collection merely to preserve a coding style.

    Keep source consistency in view

    Two fresh streams do not automatically guarantee two identical observations. If the source changes between calculations, the results may describe different states.

    For a report, decide whether that is acceptable. A count displayed beside a list should usually refer to that list, rather than to an earlier or later version of the source.

    Also distinguish reusable collections from sources that involve external resources or one-time consumption. Reopening a file or repeating a database operation is a separate decision, with its own cost and lifecycle.

    The stream abstraction does not remove those responsibilities.

    Avoid hiding required work inside side effects

    Java streams are lazy, and implementations can optimize away stages when doing so does not affect the required result. The API therefore warns against relying on incidental side effects in intermediate operations.

    For example, do not make essential business updates depend on a debugging-style action inside a pipeline whose main purpose is counting.

    Keep required state changes explicit and testable. Use stream operations to express the calculation, and make any separate update policy visible in the surrounding design.

    A good review question is simple: what is consumed, what is retained, and what may be repeated? Answering those questions produces clearer code than trying to make a consumed stream behave like a collection.

    Sources: Oracle: Stream API · Oracle: java.util.stream package

    Views: 8
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    sevketayaksiz
    • Website

    Related Posts

    Java Array Copies: A New Array Does Not Always Mean Independent Objects

    November 3, 2026

    Java String Replacement: When to Use Literal Text and When to Use Regex

    November 2, 2026

    Java Periodic Tasks: Choose the Schedule and the Failure Policy Together

    October 31, 2026
    Add A Comment

    Comments are closed.

    Şevket Ayaksız
    Instagram
    • Home
    • Adobe
    • microsoft
    • java
    • Oracle
    • Contact
    © 2026 Theme Designed by Şevket Ayaksız.

    Type above and press Enter to search. Press Esc to cancel.