Java 8 Functional interfaces and Stream API

Shihara Dilshan
3 min readAug 8, 2021

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.

--

--

Shihara Dilshan

Associate Technical Lead At Surge Global | React | React Native | Flutter | NodeJS | AWS | Type Script | Java Script | Dart | Go