Java 8 Function interface

Posted By : Lokesh Babu Sharma | 09-Dec-2020

INTRODUCTION

 

In this blog, we try to know about the Function interface. The function is a functional interface. It takes an argument of Type T and return object of Type R. The type of argument and the returning object can be different.

 

  • T: the type of the input argument
  • R: the return type of the function

 

@FunctionalInterface
public interface Function<T, R> {

      R apply(T t);

}

 

METHOD

there are four methods presented in the Function interface.

1. apply()

2. applyThen()

3. compose()

4. identity()

 

Also Read: Improving User Experiences With Progressive Web App 

 

apply()

This method applies to only the given argument and returns the result of type R.

Syntax : R apply( T t )

 

import java.util.function.Function;

public class Example {

    public static void main(String[] args) {

        Function<String, Integer> func = x -> x.length();

        Integer apply = func.apply("Hello");

        System.out.println(apply); //prints 5

    }

}

 

applyThen()

This is a chain method of Function interface. First andThen method's argument function will be executed and after that apply executes.

Syntax : <V> Function<T, V> andThen(Function<? super R, ? extends V> after)

Where V is the type of output of the after function, and of the composed function.

 

import java.util.function.Function;

public class Example {

    public static void main(String[] args) {

        Function<String, Integer> func = x -> x.length();

        Function<Integer, Integer> func2 = x -> x * 2;

        Integer result = func.andThen(func2).apply("Hello");

        System.out.println(result); // prints 10

    }

}

 

compose()

This method is the wise versa of applythen() method.

Syntax : <V> Function<V, R> compose(Function<? super V, ? extends T> before)

Where V is the type of output of the after function, and of the composed function.

 

import java.util.function.Function; 
  
public class Example { 
    public static void main(String args[]) 
    { 
        Function<Integer, Double> half = a -> a / 2.0; 
    
        half = half.compose(a -> 3 * a); 
    
      System.out.println(half.apply(5)); //prints 7.5 
    } 
} 

 

identity() 

This method of Function interface returns a function that returns its only argument.

Syntax : static <T> Function<T, T> identity()

 

import java.util.function.Function; 
  
public class Example { 
    public static void main(String args[]) 
    { 
        Function i = Function.identity(); 
  
        System.out.println(i); // prints java.util.function.Function$$Lambda$1/250421012@119d7047
    } 
} 

 

CONCLUSION

The above function interface is present in the Java 8 API. We can use these as lambda expressions.

 

We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at [email protected].

About Author

Author Image
Lokesh Babu Sharma

Lokesh is in backend team. He Believes in smart work, He is good in programming. He loves to play with codes.He has expertise in Java.

Request for Proposal

Name is required

Comment is required

Sending message..