Logo

dev-resources.site

for different kinds of informations.

What is Java Stream and why does it exist?

Published at
2/26/2024
Categories
java
streams
Author
lucasstoller
Categories
2 categories in total
java
open
streams
open
Author
12 person written this
lucasstoller
open
What is Java Stream and why does it exist?

A Stream is a sequence of values. The java.util.stream package defines types for streams of reference values (Stream) and some primitive values (IntStream, LongStream, and DoubleStream).

Streams are like iterators in the way they supply their elements as needed for processing.

Streams were introduced in Java 8 as a way to add manageability to existing data structures. With them, we can use some very interesting methods for our structures:

  • Map

  • Reduce

  • Filter

  • Find

  • Foreach

  • Among others.

For this, it is important to create a stream pipeline. A stream pipeline is used to manage actions on a stream. It is composed of iterators and finalizers, where iterators are methods that return stream and finalizers do not return streams and still close it. Example:

To use Java streams we need to convert the data structure we are working on to a Stream, assemble and run our pipeline. Finally, we can convert the stream pipeline output back to the source structure if necessary.

You might be wondering: "Why weren't such methods declared directly in the data structures?"

As you can see, we need to convert the structure we are working on to a stream before using the utility methods because they are not present in the Collections API but have been externalized in their own Streams API. And these are some of the reasons why Java Architects proposed this solution:

  1. The Collections API is more concerned with storing the data than with the actions and work that we are going to do on the data itself;

  2. Manipulation X Management of the Data Structure

  3. Eager Loading X Lazy Loading

  4. Even though those methods we declared in Collections API we would still need to do a conversion to work with asynchronous processing, see:

  5. The risk of collision in method names increases every time we insert a new method in an API with hierarchically ordered classes.

streams Article's
30 articles in total
Favicon
How does Optional.ifPresent() differ from Optional.orElse()?
Favicon
Beyond the Basics: Mastering Streams in Node.JS
Favicon
The streaming bridges — A Kafka, RabbitMQ, MQTT and CoAP example
Favicon
Java Streams | What is the difference between sorted() and distinct() in streams?
Favicon
How to handle large file uploads in SvelteKit using streams
Favicon
Execução preguiçosa com Lambdas
Favicon
Руководство по Java 8 Stream API
Favicon
In Java how to create a custom ArrayList that doesn't allow duplicate? #Interview Question
Favicon
Why Set Doesn't Allow Duplicates in Java
Favicon
Optional Class in Java and its methods
Favicon
A Few About: Streams NodeJs (PT-BR)
Favicon
File reading in python
Favicon
Asynchronous Streams in C#: Effortless Async Data Processing
Favicon
Working with streams in Node.js
Favicon
Migrating to Phoenix Liveview Streams
Favicon
What is Java Stream and why does it exist?
Favicon
-
Favicon
Streams em Node.js para iniciantes (guia básico)
Favicon
From a data stream to the data warehouse
Favicon
Gentle Intro to Node Streams
Favicon
The Java `.boxed()` Method
Favicon
Request and Response Stream - Observations
Favicon
Getting a near-real-time view of a DynamoDB stream with Python
Favicon
Exploring Java Streams
Favicon
Service Worker Side Rendering (SWSR)
Favicon
Listening to payments (real-time) on your Stellar wallet
Favicon
Supporting Cross Node Interactive Queries In Kafka Streams
Favicon
Summing Java Streams Api
Favicon
Handling Slow Servers in NodeJS
Favicon
Java 8 Non ce n’est pas que les streams

Featured ones: