Java 8 Function Interface

7 months ago codedestine 0

In this tutorial, we will learn about Java 8 Function interface.

Java 8 Function Interface

Function Interface :-

Function ( java.util.function.Function ) is a functional interface that has one abstract method and two default method declared in it. It represents an algorithm where input parameter of one type is used to produce return value of same or another type.

Abstract method :- 

R  apply ( T t ) :- The abstract method apply ( T t ) is a functional method. It accepts one parameter as an argument and return one parameter R as a return value. It represents an algorithm or a business logic, where a passed argument T is used to produce a return value R.

Default method :-  

  1. default  <V>  Function<T,R>   andThen ( Function <? super R, ? extends V> after ) :- It returns a function that performs two operation in sequence, first the operation of a function on which method is called, this operation will take one parameter of type T as an argument and return one parameter of type R as a return value. Second, the operation of a function passed as an argument in andThen method, this operation will take one parameter of type R ( Generated during first operation as an argument and return one parameter of type V as a return value.
  2. default  <V>  Function<R,V>   compose ( Function <? super T, ? extends R> after ) :- It returns a function that performs two operation in sequence, first the operation of a function passed as an argument in compose method, this operation will take one parameter of type T as an argument and return one parameter of type R as a return value. Second, the operation of a function on which method is called, this operation will take one parameter of type R ( Generated during first operation as an argument and return one parameter of type V as a return value.

Static method :- 

static <T> Function<T,T> identity() :- It returns a function that always returns its input argument

Reference :-  Function Interface JavaDocs 

Stream API have few methods where Java 8 Function interface are used as an argument, they are as follows :-

  1. <R> Stream<R>  map ( Function<? super T, ? extends R> mapper )
  2. <R> Stream<R>  flatMap ( Function<? super T, ? extends Stream<? extends R>> mapper )

There are twelve specialised ( primitive specific ) form of Java 8 Function interface, which can be used in case of int, long and double. They are as follows :-

  1. IntFunction<R> interface ( JavaDocs ) :- It represents a function that accepts one integer value as an argument and returns a value of type R.
  2. DoubleFunction<R> interface ( JavaDocs ) :- It represents a function that accepts one double value as an argument and returns a value of type R.
  3. LongFunction<R> interface ( JavaDocs ) :- It represents a function that accepts one long value as an argument and returns a value of type R.
  4. IntToDoubleFunction interface ( JavaDocs ) :- It represents a function that accepts one integer value as an argument and returns a double value.
  5. IntToLongFunction interface ( JavaDocs ) :- It represents a function that accepts one integer value as an argument and returns a long value.
  6. DoubleToIntFunction interface ( JavaDocs ) :- It represents a function that accepts one double value as an argument and returns an integer value.
  7. DoubleToLongFunction interface ( JavaDocs ) :- It represents a function that accepts one double value as an argument and returns a long value.
  8. LongToIntFunction interface ( JavaDocs ) :- It represents a function that accepts one long value as an argument and returns an integer value.
  9. LongToDoubleFunction interface ( JavaDocs ) :- It represents a function that accepts one long value as an argument and returns a double value.
  10. ToIntFunction<T> interface ( JavaDocs ) :- It represents a function that accepts one parameter of type T as an argument and returns an integer value.
  11. ToDoubleFunction<T> interface ( JavaDocs ) :- It represents a function that accepts one parameter of type T as an argument and returns a double value.
  12. ToLongFunction<T> interface ( JavaDocs ) :- It represents a function that accepts one parameter of type T as an argument and returns a long value.

Example – 1 :- 

This example will show you, how to create and call different methods of a Java 8 function interface.

  public static void main(String[] args){
		
    Function<String,Integer> function1 = ( t ) -> { 
                                                   if(t != null){
                                                     if(t.equalsIgnoreCase("Manager")){
                                                       return 1000000;
                                                     }else if(t.equalsIgnoreCase("Developer")){
                                                       return 100000;
                                                     }else {
                                                       return 0;
                                                     }
                                                   }else{
                                                     return 0;
                                                   }
                                                 };
		  
    int salary = function1.apply("Manager");
    System.out.println(salary);
	  
    salary = function1.apply("Developer");
    System.out.println(salary);
	  
    Function<Integer,String> function2 = ( t ) -> { if(t >= 1000000){
                                                      return "Band 5";
                                                    }else if(t >= 100000){
                                                      return "Band 4";
                                                    }else{
                                                      return "Band 3";
                                                    }
                                                  };
		 
     // Example of andThen method	 
     String band1 = function1.andThen(function2).apply("Manager");
     System.out.println(band1);
	   
     // Example of compose method	
     String band2 = function2.compose(function1).apply("Developer");
     System.out.println(band2);
  }  

Result :- 

1000000
100000
Band 5
Band 4

Example – 2 :- 

In this example, we will multiple every element of a list by 2, using stream’s map() method. This method takes one function as an argument.

  public static void main(String[] args){
 
    List<Integer> intList = Arrays.asList(1, 2, 3, 4);
 
    // Map method Example
    List<Integer> newList = intList.stream().map( (e) -> e * 2 ).collect(Collectors.toList()); 
    newList.forEach(System.out::println);
 }

Result :- 

2
4
6
8

That’s all for Java 8 Function Interface. If you liked it, please share your thoughts in comments section and share it with others too.