Java 8 Functional Interface Examples

Category : Java | Sub Category : Java 8 Features | By Runner Dev Last updated: 2020-09-30 12:50:44 Viewed : 224


Java 8 Functional Interface :

Syntax:
 Conceptually, a functional interface has exactly one abstract method (e.g. Runnable, Callable, Comparator).

Pre-Built function library (java.util.function) :

There are a lot of re-usable functional requirements that can be captured by functional interfaces and lambdas. The designers of Java 8 have captured the common use cases and created a library of functions

·       java.lang.FunctionalInterface:

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface FunctionalInterface {}

·       java.util.Function:

@FunctionalInterface

public interface Function<T, R> { }

·       java.util.function.Function<String, Integer>

@FunctionalInterface

  functional method is apply(Object).

Type Parameters:

<T> the type of the input to the function

<R> the type of the result of the function

 

Examples:

1. Function<T, R>:

  FunctionExample.java   

 import java.util.function.Function; 

public class FunctionExample {

     public static void main(String[] args) {

           Function<String, Integer> stringToInt = x -> Integer.valueOf(x);

           Integer applyint = stringToInt.apply("11");

           System.out.println(applyint);// 11

 

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

           Integer applyStr = length.apply("runnerdev"); // 9

           System.out.println(applyStr);

     }

}

 


 OutPut:

        11

        9

2. Chain Function<T, R>

ChainFunctionExample.java

 

 import java.util.function.Function;

 

public class ChainFunctionExample {

     public static void main(String[] args) {

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

             Function<Integer, Integer> function2 = x -> x * 9;

             Integer result = funtion1.andThen(function2).apply("runnerdev");   // 81

             System.out.println(result);

     }

}

 


 Output:

    81

3.  convert a List into a Map:

ListToMapConvertFuncEx.java 

 import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.function.Function;

 

public class ListToMapConvertFuncEx {

     public static void main(String[] args) {

           ListToMapConvertFuncEx object = new ListToMapConvertFuncEx();

 

           List<String> list = Arrays.asList("Ant""An Elephant""Animal""Dummy");

 

          // lambda expression implementation

           Map<String, Integer> map = object.convertListToMap(listx -> x.length());

 

           System.out.println(map); // {An Elephant=11, Ant=3, Animal=6, Dummy=5}

 

           // method reference implementation

           Map<String, Integer> map2 = object.convertListToMap(listobject::getLength);

 

           System.out.println(map2);//{An Elephant=11, Ant=3, Animal=6, Dummy=5}

     }

 

     public <T, R> Map<T, R> convertListToMap(List<T> list, Function<T, R> func) {

 

           Map<T, R> result = new HashMap<>();

           for (T t : list) {

                result.put(tfunc.apply(t));

           }

           return result;

 

     }

 

     public Integer getLength(String str) {

           return str.length();

     }

}

 


 OutPut:

    {An Elephant=11, Ant=3, Animal=6, Dummy=5}

    {An Elephant=11, Ant=3, Animal=6, Dummy=5}

Search
Related Articles

Leave a Comment: