Member-only story

Useful Java Streams

theoneamin
2 min readMay 7, 2022

--

In this article, we’ll take a look at some useful ways you can use Java Streams to work with different data.

1. Comma-separated string to List.

Let’s say we have a comma-separated string of numbers. Without having to write many lines of code, we can get a list of numbers as per our example.

and the result looks like this:

2. Find from List

Let’s say we want to find a specific name from a list of names.

we can use Java streams to find any name with value the “Amin”

This will give us a string with the value “Amin” or null if it does not exist.

3. Sum from a list of objects

Let’s say we have a list OrderDTO objects where each object has a total amount. If we want to sum the total of all orders, we can make use of map and reduce.

--

--

theoneamin
theoneamin

Responses (4)

Write a response