What is the Function Interface in Java 8

Last updated on February 29th, 2024

Function Interface is a predefined functional interface introduced in Java 8. If we can give some input then perform some operation on that given input and provide a corresponding output. We don’t want to conditional check but we need some business logic.
Like: Given input 4 —->  square(4) —–>16
So, our requirement is business functionality whenever we can provide input and perform some operation then the corresponding output is provided then we can use the “Function” predefined functional interface.
Prototype:

interface Function<T , R>{

public R apply(T t);

}

Type Parameters:
T – It is a type of input to the function
R – It is a type of result of the function
Example:

import java.util.function.Function;

public class Test {

public static void main(String[] args) {

Function<Integer, Integer> f = s -> s * s;

System.out.println("Square Of No.:" + f.apply(4));

System.out.println("Square Of No.:" + f.apply(5));
    }
}

→ In the above we can achieve the square of a given number task.
→ We analyzed the lines of code concisely by using the “Function” functional interface.
Output:

Square Of No.:16
Square Of No.:25

Example: 

import java.util.function.Function;

public class Test {

public static void main(String[] args) {

Function<String, Integer> f = s -> s.length();

System.out.println("Square Of No.:" + f.apply("Functional Interface"));

}
}

→ In the above example, we gave input as a String and performed an operation i.e., calculate a String length business logic and provide the result of the length of a String.
Output:

Square Of No.:20

Example: 

import java.util.function.Function;

class Student {

  String name;

  int marks;

  public Student(String name, int marks) {

    this.name = name;

    this.marks = marks;

  }

}

public class Test {

  public static void main(String[] args) {

    Function < Student, String > f = stdnt -> {

      int marks = stdnt.marks;

      String grades = "";

      if (marks >= 90)

        grades = "A+";

      else if (marks >= 80)

        grades = "A";

      else if (marks >= 70)

        grades = "B+";

      else if (marks >= 60)

        grades = "B";

      else if (marks >= 50)

        grades = "C";

      else if (marks >= 40)

        grades = "D";

      else

        grades = "E";

      return grades;

    };

    Student[] s = {
      new Student("Raghav", 100),
      new Student("Raj", 80),
      new Student("Varun", 70),

      new Student("Vikas", 60),
      new Student("Manav", 50),
      new Student("Dinesh", 40),

      new Student("Deepak", 30)
    };

    for (Student s1: s) {

      System.out.println("Student Name:" + s1.name);

      System.out.println("Student Marks:" + s1.marks);

      System.out.println("Student Grade:" + f.apply(s1));

      System.out.println("------------------------------");

    }

  }

}

→ In the above we can use our own class type input in the “Function” functional interface.
Output:

Student Name:Raghav
Student Marks:100
Student Grade:A+
------------------------------
Student Name:Raj
Student Marks:80
Student Grade:A
------------------------------
Student Name:Varun
Student Marks:70
Student Grade:B+
------------------------------
Student Name:Vikas
Student Marks:60
Student Grade:B
------------------------------
Student Name:Manav
Student Marks:50
Student Grade:C
------------------------------
Student Name:Dinesh
Student Marks:40
Student Grade:D
------------------------------
Student Name:Deepak
Student Marks:30
Student Grade:E
------------------------------

Function Chaining:
Two Functions combined together to perform a complex operation are called “Function Chaining”.

f1.andThen(f2).apply(i); //first f1 followed by f2
f1.compose(f2).apply(i); //first f2 and then f1

Example: 

import java.util.function.Function;

public class Test {

  public static void main(String[] args) {

    Function < Integer, Integer > f1 = i -> 2 * i;

    Function < Integer, Integer > f2 = i -> i * i * i;

    System.out.println(f1.andThen(f2).apply(2));

    System.out.println(f1.compose(f2).apply(2));

  }
}

→ f1.andThen(f2).apply(2): In this case, f1 apply first then f2 is applied (i.e., 2*2=4 and then 4*4*4=64).
→ f1.compose(f2).apply(2): In this case f2 applies first then f1 is applied (i.e., 2*2*2=8 and then 2*8=16).
Output:

64
16

Conclusion:
This example is explained What is the Function predefined functional interface? How to use this Function functional interface? What is the Function of chaining? How to use Function chaining?

Leave a Comment