Home core java Java 8 Function and examples

Java 8 Function and examples

1. What is Function interface in Java?

Java 8 Function interface is exactly same as Predicate except that functions can return any type of result but function should(can) return only one value and that value can be any type. This is a functional interface whose functional method is apply(Object).

Function interface present in java.util.function package. As java.util.function.Function is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

2. Java 8 Function interface methods :

Modifier and TypeMethod and Description
default Function<T,V>andThen(Function<? super R,? extends V> after) – Returns a composed function that first applies this function to its input, and then applies the after function to the result.
Rapply(T t) – Applies this function to the given argument.
default Function<V,R>compose(Function<? super V,? extends T> before)– Returns a composed function that first applies the before function to its input, and then applies this function to the result.
static Function<T,T>identity() – Returns a function that always returns its input argument.

2.1. Java 8 Function apply() example

Following example shows you how to use apply()using lambdas.

class FunctionDemo {
    
    public static void main(String[] args) {
        
        /*apply(T t) -Applies this function to the given argument,
          and then returns any type. */
        
        Function<Integer, Integer> f1 = i -> i*4;
        System.out.println(f1.apply(3));//12
        
        Function<Integer, Integer> f2 = i -> i+4;  
        System.out.println(f2.apply(3));//7
        
        Function<String, Integer> f3 = s -> s.length();
        System.out.println(f3.apply("Peter"));//5
        
    }   
}

2.2. Function compose() example

Following example shows you how to use compose(), and how to compose multiple functions.

- Advertisement -
class FunctionDemo2 {
    
    public static void main(String[] args) {
        
        /* compose(Function<? super V,? extends T> before) -
          Returns a composed function that first applies the before function to its input, 
          and then applies this function to the result.
         */
        
        Function<Integer, Integer> f1 = i -> i*4;
        Function<Integer, Integer> f2 = i -> i+4;  
        
        System.out.println(f2.compose(f1).apply(3)); // 16
        
        //f2.compose(f1).apply(3) - is equals to following statements
        Integer j1 = f1.apply(3);
        Integer j2 = f2.apply(j1);
        
        System.out.println(j2); //16     
    }   
}

2.3. Function andThen() example

Following example shows you how to use andThen() using lambdas.

class FunctionDemo3 {
    
    public static void main(String[] args) {
        
        /* andThen(Function<? super R,? extends V> after) -
           Returns a composed function that first applies this function to its input,
           and then applies the after function to the result.
         */
        
        Function<Integer, Integer> f1 = i -> i*4;
        
        Function<Integer, Integer> f2 = i -> i+4;  
        
        System.out.println(f2.andThen(f1).apply(3)); // 28
        
        //f2.andThen(f1).apply(3) - is equals to below statements
        Integer j1 = f2.apply(3);
        Integer j2 = f1.apply(j1);
        
        System.out.println(j2); //28
        
    }   
}

2.4. identity() method example

identity() – Returns a Function that always returns its input argument.

Following example shows you how to use identity() and for more example you can refer Java 8 identity function Function.identity examples article.

System.out.println(Function.identity().apply(10));//10 
System.out.println(Function.identity().apply("Peter"));//Peter

Conclusion :

In this article we covered Java 8 Function interface and all the methods of Function interface with simple basic examples.

- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles