Some commonly used Functional Interfaces

Reading Time: 3 minutes

Hello, friends as we know Java achieved functional programming using a special type of interface called functional interfaces and Lambda Expressions. In this blog, I will discuss the four most used predefined functional interfaces.

But before going into that let’s see what is Functional Interface ? so the answer is any interface with a Single Abstract Method is a functional interface. It is good practice to write a notation @FunctionalInterface at the top of every functional interface as it gives an error when we try to create any other abstract method inside the interface. So that in the future you do not create any other abstract method inside that interface.

@FunctionalInterface
public interface LengthOfString {    public int lengthOfString(String str);}

The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation.

Most used functional interfaces :

  • Predicate
  • Consumer
  • Function
  • Supplier
                                                                most used Functional Interfaces

Predicate :-

the predicate has a method test that takes an object of type T and it is used to test some conditions that’s why it returns a boolean value. let’s see through an example.

@FunctionalInterface
public interface Predicate<T> {   
    boolean test(T t);
}
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class PredicateImpl {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("SHIVAM");
        list.add("");
        list.add("GOPAL");
        list.add("SATYAM");
        list.add("");
        list.add("ABHISHEK");
        list.add("SHIVAM-RAI");
        Predicate<String> predicate = str-> !str.isEmpty();
        List<String> newList = filterList(list,predicate);
        System.out.println(newList);
        List<Integer> intList = List.of(1,4,6,7,8,9);
        Predicate<Integer> evenList = num-> num%2==0;
        List<Integer> evens = filterList(intList, evenList);
        System.out.println(evens);
        Predicate<String> predicate1 = str->str.contains("SHIVAM");
        List<String> filterList = filterList(list,predicate1);
        System.out.println(filterList);
    }

    private static <T> List<T> filterList(List<T> list, Predicate<T> predicate) {
     List<T> newList =  new ArrayList<>();
     for(T string:list){
         if(predicate.test(string)){
             newList.add(string);
         }
     }
     return newList;
    }
}
Output:
[SHIVAM, GOPAL, SATYAM, ABHISHEK, SHIVAM-RAI]
[4, 6, 8]
[SHIVAM, SHIVAM-RAI]

Here we can see that whenever we need to create a function that takes only a single argument and returns the boolean value we can use predefined function test of predicate interface it makes our code simple and concise.

Consumer :-

It is called consumer because it consumes. This functional interface defines the abstract method. It takes objects of generic type T and returns nothing that is void so the consumer just consumes something and returns nothing. A consumer can be any operation that takes something do something with it but does not return anything like we can print elements using the consumer.

@FunctionalInterface
public interface Consumer<T> {   
    void accept(T t);
}
import java.util.List;
import java.util.function.Consumer;

public class ConsumerImpl {
    public static void main(String[] args) {
        List<Integer> list = List.of(34,26,37,43,52,68);
        Consumer<Integer> consumer = num-> System.out.println(num);
        printElement(list,consumer);
        List<String> list1 =List.of("RAHUL","AJAY","SHIVAM");
        Consumer<String> consumer1 = val-> System.out.println(val);
        printElement(list1,consumer1);

    }

    private static <T> void printElement(List<T> list, Consumer<T> consumer) {
        for (T num:list) {
           consumer.accept(num);
        }
    }
}
Output:
34
26
37
43
52
68
RAHUL
AJAY
SHIVAM

Function :-

A function is a functional interface that can take something and can return something. Generally, the function is used for transformation. Function have method apply. It takes an object of generic type T and transforms it to an object of generic type R. It is not using T at both places. So the return object can be of the same type or it can be of a different type as well.

@FunctionalInterface
public interface Function<T,R> {   
    R accept(T t);
}
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class FunctionImpl {
    public static void main(String[] args) {
        List<String> list =List.of("RAHUL","AJAY","SHIVAM");
        Function<String,Integer> function = str->str.length();
        List<Integer> newList = map(list,function);
        System.out.println(newList);
    }

    private static <T,R> List<R> map(List<T> list, Function<T, R> function) {
        List<R> newList = new ArrayList<>();
        for (T str:list) {
           newList.add(function.apply(str));
        }
        return newList;
    }
}
Output:
[5, 4, 6]

Supplier :-

The supplier has a method get that takes nothing but returns something so this interface is used to produce results without taking any input.

@FunctionalInterface
public interface Supplier<R> {   
    R get();
}
import java.util.function.Supplier;

public class SupplierImpl {
    public static void main(String[] args) {
        Supplier<String> stringSupplier = ()-> new String("SHIVAM");
        System.out.println(stringSupplier.get());
        Supplier<Double> randomNumber = ()-> Math.random();
        System.out.println(randomNumber.get());
    }
}
Output:
SHIVAM
0.8810323475052316

Thank you so much for reading my blog hope you have liked it. To know more about functional Interfaces you can visit the link.

Written by 

I'm a Software Consultant at Knoldus . I have completed my B.tech in Computer Science stream from IMS Engineering College, Ghaziabad. I love to explore new technologies and have great interest in problem solving.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading