IntFunction is a functional interface, it takes an int and returns a reference type.
This reference type can be specified by using generic type specification.
We can implement it in different ways.
Implement using a regular class.
private class IntFuncImpl implements IntFunction<Integer>{ @Override public Integer apply(final int intValue) { return Integer.valueOf(intValue); } }
Implement using an anonymous class.
final IntFunction<Integer> anonClass = new IntFunction<Integer>() { @Override public Integer apply(final int intValue) { return Integer.valueOf(intValue); } };
Lambda 1 :-
final IntFunction<Integer> lambdaWithDataType = (int intValue) -> { return Integer.valueOf(intValue); };
Lambda 2 :-
final IntFunction<Integer> lambdaWithoutDataType = (intValue) -> Integer.valueOf(intValue);
Lambda 3 :-
final IntFunction<Integer> lambdaWithMethodReference = Integer::valueOf;
You can watch my youtube video, where I am coding this implementation.
Full Source Code :-
import java.util.function.IntFunction; public class IntFunctionImpl{ private class IntFuncImpl implements IntFunction<Integer>{ @Override public Integer apply(final int intValue) { return Integer.valueOf(intValue); } } final IntFunction<Integer> anonClass = new IntFunction<Integer>() { @Override public Integer apply(final int intValue) { return Integer.valueOf(intValue); } }; final IntFunction<Integer> lambdaWithDataType = (int intValue) -> { return Integer.valueOf(intValue); }; final IntFunction<Integer> lambdaWithoutDataType = (intValue) -> Integer.valueOf(intValue); final IntFunction<Integer> lambdaWithMethodReference = Integer::valueOf; public static void main(final String... args){ final IntFunctionImpl obj = new IntFunctionImpl(); final IntFuncImpl func = obj.new IntFuncImpl(); Integer val = func.apply(19); System.out.println(val); val = obj.anonClass.apply(10); System.out.println(val); val = obj.lambdaWithDataType.apply(10); System.out.println(val); val = obj.lambdaWithoutDataType.apply(12); System.out.println(val); val = obj.lambdaWithMethodReference.apply(23); System.out.println(val); } }