Your Privacy

This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies.
COOKIE POLICY

Skip to main content

Keep Calm and Dev On | Streams API

Keep Calm and Dev On | Streams API
Back to insights

One of my favorite Java 8 features is the introduction of the Streams API. Not to be confused with Input/Output Streams, the Streams API is Java’s ‘new’ way to process data that is expressive, efficient and, relatively speaking, more functional than ever.  Streams are new in the sense that Lambda functions were new to Java when version 8 was released in 2014, but on the other hand, the notion of Lambdas are not really anything ‘new’ in the context of functional programming.

Without further ado, check out this super simple example and see if you can figure out what it does.  Streams are intentionally expressive.  The idea is that you can read the code and just understand what is going on.

Java Code Block:

List<Integer> numberList = new ArrayList<>(Arrays.asList(

new Integer[] {1, 2, 3, 4, 5, 2, 4}

));

Set<Integer> evenNumberSet = numberList.stream()

.filter(number -> isEven(number))

.filter(number -> isGreaterThanThree(number))

.collect(Collectors.toSet());

Ok, so what is going on here?  Well, if you guessed that we start with a list of integers, filter out the non-even ones, then filter out results not greater than three, then reduce the unique results to a new Set, then you are correct!  Although this is among the simplest examples, I think it still illustrates just how expressive the Stream API is.  It reads like a book, right to left, top to bottom.

Right about now, a skeptical Java dev concerned about performance might say, “Great, Streams are expressive, but are they efficient?”  I’m here to tell you to ‘keep calm and dev on’ because the answer is (generally) yes, Streams are efficient.  To understand how they are efficient we need to go under the hood a little bit.  In a nutshell, here’s how it works:

The Streams API is composed of a few intermediate operations and a few terminal operations.  The intermediate operations are functions like ‘filter’ and ‘map’.  We can use these intermediate operations to solve a standard server-side use case, like when we need to change/process a collection of data in some way.  Intermediate operations can be chained together, one after the other, because they return Streams respectively.

Then, after all the intermediate operations, a Stream always ends with a terminal operation like ‘count, ‘findAny’ or ‘collect.  In other words, once we’re done processing our data, let’s get a count of the results, find a result of interest or return the results in the form of a new Collection.

Now, the interesting thing about the intermediate and terminal operations is that in the code, the intermediate operations appear to be called first, and then the terminal operation appears to be called last.  However, at run time, the intermediate operations don’t actually get executed until the terminal operations are invoked.  This concept is called lazy evaluation.  Anytime you see lazy evaluation, you can know that you’re saving precious CPU clock cycles by processing data in an ‘on-demand’ fashion.

Lastly, I’d just like to say that I really love the addition of Lambda Functions to Java.  Lambdas are great because they give Java that functional look and feel that it had always been missing.  Lambdas and the Streams API have given us the power to create complex, composable data processing, while allowing us to keep our code readable, self-documenting and maintainable.  Perhaps my favorite part of all is that anytime you do something with a Stream, regardless of complexity, it’s technically always a one liner!  Tell me who doesn’t love that?!?

Digging In

  • Digital Products

    TAG Panel: Differentiate Your Customer Experience

    Join the CX and Product Management Societies to hear from our panel of Human-Centered Design experts on the business value of Agentic AI.

  • Digital Products

    The Bloated SaaS Era: Paying More for Less While Businesses Wait

    SaaS was supposed to make business faster, smarter, and more efficient. Instead, it’s become bloated, expensive, and painfully slow to change. The platforms we rely on—Salesforce, Workday, SAP, and others—haven’t truly innovated in years. Yet, they demand massive investments in re-implementation, process re-engineering, and data migration just to keep up. It’s time to ask: Are […]

  • Digital Products

    Reid Braswell Joins UDig as Vice President, Software Engineering

    UDig is proud to welcome Reid Braswell as our new Vice President of Software Engineering. With over 13 years of experience in technology consulting, Reid brings deep expertise in digital transformation, modern software engineering, and client-focused solutions. His leadership and passion for solving complex challenges make him an exceptional addition to the UDig team. Reid’s […]

  • Digital Products

    Energy 2025 – Expansion of Fossil Fuels or Carbon Reduction?

    Now that the election is behind us, we have an opportunity to anticipate the possible effects on the energy industry under this new administration. What strategies will be impacted? What will remain the same? What opportunities can we take advantage of in 2025? This blog is meant to dig into these questions and provide some […]

  • Digital Products

    The Growing Importance of Digital Accessibility

    Embracing Digital Accessibility: A Moral and Business Imperative In today’s digital landscape, accessibility has become crucial for businesses and organizations. With increasing awareness and legal requirements, ensuring that digital products are accessible to all users, including those with disabilities, is not just a compliance issue but a moral imperative. At UDig, we champion ADA compliance […]

  • Digital Products

    Unlocking Business Potential: The Power of Custom Application Development

    Like any savvy business leader, you’re likely always on the lookout for tools to give your company a competitive edge. And in doing so, you’ve undoubtedly considered investing in custom application development. But the question is, how do you ensure that such a major investment in a custom web application development provides a strong return on […]