Mastering the new functional Java

The Bored Dev
codeburst
Published in
12 min readJun 28, 2020

--

In my last article, we talked mostly about how Java Streams work and we also had an introduction to Functional Programming in the article “ A new Java functional style “; now it’s time to take these learnings and see how we can use Streams and take all their benefits.

We’ll go through a set of examples with different complexities to try to show how Java Streams and functional programming can help us solve complex problems that we could face in our day-to-day work as developers.

Let’s see how can we use Java Streams then!

How to use Streams

As you could expect, to start using Streams the first thing we need to know is how to create a stream! Let’s see the different ways of creating a Java Stream.

Creating a Stream

There are multiple ways of creating a Stream in Java, let’s go quickly through some of them:

  • From a collection
    We can convert a collection into a stream by using stream() and parallelStream().
  • From an array
    A stream can be created from an array by using the factory methods Arrays.stream or Stream.of
  • Using a generator function
    There are different ways to create a generator function in Java. For example by using Stream.iterate we can create an infinite…

--

--