Home  >  Java 8

Java 8 Function Examples

By Arvind Rai, November 23, 2014
Java 8 provides function API . These API are functional interface. We can assign it as lambda expression. Function accepts arguments, perform some processing and finally produces result. We need to define data type of arguments and result. Java 8 provides different in-built functions for different data type. In this page we will see many examples for different functions.

Function<T,R>

Function produces result of given data type and accepts one argument as given data type.
FunctionExample.java
package com.concretepage.util.function;
import java.util.function.Function;
public class FunctionExample {
    public static void main(String[] args) {
        Function<Integer,String> ob = f1 -> "Age:"+f1;
        System.out.println(ob.apply(20));
    }    
}



DoubleFunction<R>

Accepts only double data type value and returns the result as given data type.
DoubleFunctionExample.java
package com.concretepage.util.function;
import java.util.function.DoubleFunction;
public class DoubleFunctionExample {
    public static void main(String[] args) {
        DoubleFunction<String> df = d -> String.valueOf(d*5.3); 
        System.out.println(df.apply(43.7));
    }    
}

DoubleToIntFunction

Accepts double data type value and returns integer value.
DoubleToIntFunctionExample.java
package com.concretepage.util.function;
import java.util.function.DoubleToIntFunction;
public class DoubleToIntFunctionExample {
    public static void main(String[] args) {
        DoubleToIntFunction ob = f -> new Double(f*4.8).intValue();
        System.out.println(ob.applyAsInt(43.7));
    }    
}

DoubleToLongFunction

Accepts double data type value and returns long data type value.
DoubleToLongFunctionExample.java
package com.concretepage.util.function;
import java.util.function.DoubleToLongFunction;
public class DoubleToLongFunctionExample {
    public static void main(String[] args) {
        DoubleToLongFunction ob = f -> new Double(f*4.8).longValue();
        System.out.println(ob.applyAsLong(43.7));
    }    
}

IntFunction<R>

Accepts only integer value and produces result of given data type.
IntFunctionExample.java
package com.concretepage.util.function;
import java.util.function.IntFunction;
public class IntFunctionExample {
    public static void main(String[] args) {
        IntFunction ob = f -> f*f;
        System.out.println(ob.apply(43));
    }    
}

IntToDoubleFunction

Accepts integer value and produces double data type value.
IntToDoubleFunctionExample.java
package com.concretepage.util.function;
import java.util.function.IntToDoubleFunction;
public class IntToDoubleFunctionExample {
    public static void main(String[] args) {
        IntToDoubleFunction ob = f -> f*f;
        System.out.println(ob.applyAsDouble(43));
    }    
}

IntToLongFunction

Accepts integer and produces long data type value.
IntToLongFunctionExample.java
package com.concretepage.util.function;
import java.util.function.IntToLongFunction;
public class IntToLongFunctionExample {
    public static void main(String[] args) {
        IntToLongFunction ob = f -> f*f;
        System.out.println(ob.applyAsLong(43));
    }    
}

LongFunction<R>

Accepts only long data type value and produces result of given data type.
LongFunctionExample.java
package com.concretepage.util.function;
import java.util.function.LongFunction;
public class LongFunctionExample {
    public static void main(String[] args) {
        LongFunction<String> ob = f -> String.valueOf(f*f);
        System.out.println(ob.apply(43));
    }    
}

LongToDoubleFunction

Accepts long data type value and produces double data type value.
LongToDoubleFunctionExample.java
package com.concretepage.util.function;
import java.util.function.LongToDoubleFunction;
public class LongToDoubleFunctionExample {
    public static void main(String[] args) {
        LongToDoubleFunction ob = f -> f*f;
        System.out.println(ob.applyAsDouble(43));
    }    
}

LongToIntFunction

Accepts long data type value and produces integer data type value.
LongToIntFunctionExample.java
package com.concretepage.util.function;
import java.util.function.LongToIntFunction;
public class LongToIntFunctionExample {
    public static void main(String[] args) {
        LongToIntFunction ob = f -> (int)(f*f);
        System.out.println(ob.applyAsInt(43));
    }    
}

ToDoubleBiFunction<T,U>

Accepts two arguments of the given data type and produces result of double data type.
ToDoubleBiFunctionExample.java
package com.concretepage.util.function;
import java.util.function.ToDoubleBiFunction;
public class ToDoubleBiFunctionExample {
    public static void main(String[] args) {
        ToDoubleBiFunction<Integer,Integer> ob = (f1,f2) -> f1+f2;
        System.out.println(ob.applyAsDouble(102,305));
    }    
}

ToDoubleFunction<T>

Accepts the value of given data type and produces double value.
ToDoubleFunctionExample.java
package com.concretepage.util.function;
import java.util.function.ToDoubleFunction;
public class ToDoubleFunctionExample {
    public static void main(String[] args) {
        ToDoubleFunction<Integer> ob = f1 -> f1*301;
        System.out.println(ob.applyAsDouble(102));
    }    

ToIntBiFunction<T,U>

Accepts two arguments of given data type and produces result of integer data type.
ToIntBiFunctionExample.java
package com.concretepage.util.function;
import java.util.function.ToIntBiFunction;
public class ToIntBiFunctionExample {
    public static void main(String[] args) {
        ToIntBiFunction<Integer,Integer> ob = (f1,f2) -> f1+f2;
        System.out.println(ob.applyAsInt(102,306));
    }    
}

ToIntFunction<T>

Accepts an argument of given data type and produces integer value.
ToIntFunctionExample.java
package com.concretepage.util.function;
import java.util.function.ToIntFunction;
public class ToIntFunctionExample {
    public static void main(String[] args) {
        ToIntFunction<Integer> ob = f -> f*123;
        System.out.println(ob.applyAsInt(306));
    }    
}

ToLongBiFunction<T,U>

Accepts two arguments of given data type and produces long data type value.
ToLongBiFunctionExample.java
package com.concretepage.util.function;
import java.util.function.ToLongBiFunction;
public class ToLongBiFunctionExample {
    public static void main(String[] args) {
        ToLongBiFunction<Integer,Integer> ob = (f1,f2) -> f1-f2;
        System.out.println(ob.applyAsLong(306,405));
    }    
}

ToLongFunction<T>

Accepts an argument of given data type and produces result of long data type.
ToLongFunctionExample.java
package com.concretepage.util.function;
import java.util.function.ToLongFunction;
public class ToLongFunctionExample {
    public static void main(String[] args) {
        ToLongFunction<Integer> ob = f1 -> f1+50;
        System.out.println(ob.applyAsLong(306));
    }    
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us