Java 8 Functional interfaces and Stream API

Hello everyone I hope you all are doing great. Today my topic is Java 8 Functional interfaces and Stream API, which may new thing for some of you guys.

What exactly stream API?

Stream API is something new which was introduced back in March 2014 with Java 8. It is an abstract layer for the developers that can be use to process data just like SQL. Let’s take a look at a practical example and clear things up.

Let’s say you have a list of integers and you want to filter it and get all the integers that is greater than 5. Normally(before java 8/without stream API) you will probably write a solution for that like this.

But like in SQL, with Stream API you only say what you want rather than writing each and every step in code. This will make your code much cleaner and easier to understand.

Before go into further with the stream API, let’s talk about something called Functional Interfaces. What are those and how functional interfaces are linked with this Stream API.

What is a functional interface?

Functional interface is just a interface in java but only have 1 abstract method. Interfaces can have many abstract methods but in functional interface there is only one.

How a functional interface is different than a normal interface?

The main difference is you can you Lambda to instantiate a functional interface. That means you don’t need a concrete class to implement the functional interface.

In the above example I have two interfaces.

  1. Normal interface with several abstract methods.
  2. Functional interface with one abstract method.

Since you cannot instantiate an interface you have to ether implement the interface in a concrete class or you can your anonymous class. But since we have an functional interface we can use lambda expressions.

So what all these things have to do with Stream API?

If you remember previous code that we have written, The filter method take something call predicate. And predicate is an functional interface that contain a single argument function that returns a boolean value. Since predicate is an functional interface we can use lambda expressions.

.stream()
.filter(number -> number > 5)
.collect(Collectors.toList());

Let’s talk about some special functional interfaces that commonly use in Stream API.

  1. Predicate
@FunctionalInterface 
public interface Predicate<T> {
boolean test(T t);
}

Predicate is a functional interface that contain a single argument function that returns a boolean value. Predicates are commonly use in filtering.

2. Consumer

@FunctionalInterface 
public interface Consumer<T> {
void accept(T t);
}

Consumer is a functional interface that it takes an argument and returns nothing.

3. Java 8 Function

@FunctionalInterface 
public interface Function<T, R> {
R apply(T t);
}

Represents a function that accepts one argument and produces a result.

There are so many functional interfaces in Java 8. You can refer them in here.

Now it’s finally time to go deep into the Stream API.

Stream API is used to process collections of objects. Normally we use Stream API with lists. So let’s talk about some examples with a Java list.

Ex 1 : You have a list of Integers and you want to filter it and get the Integers that are greater than 5.

Ex 2 : You have a list of Integers and you want to print all of those.

myList
.stream()
.forEach(number -> System.out.println(number));//use consumer

There are so much to learn in Stream API. In this article I talked about functional interfaces and how they are related with Stream API. Also I talked about some special functional interfaces like predicates, consumers that are commonly used in Stream API. In future article I will go more deeper to Stream API and will talk about some cool things that we can do in Stream API.

Software Engineer 2 at Arimac || Data science | Machine learning | Deep learning Enthusiast || SE Undergraduate

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Containerise React Application

Aquashella Orlando 2022

How to Uninstall Apps on your Mac

Goodbye Xamarin.Forms

Implementing Operating System #5

Test your Ruby skills with Evrone Quiz!

Deploying Containerized Laravel Application Using AWS EC2 Instances With Docker And RDS

Zenity command explanation , and its options , and integration of other commands with zenity

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shihara Dilshan

Shihara Dilshan

Software Engineer 2 at Arimac || Data science | Machine learning | Deep learning Enthusiast || SE Undergraduate

More from Medium

SOLID Design in Practice — S: Single Responsibility Principle

ArrayList in Java collections

Java 學習記錄91 — exists