Java8 - Learn Numeric Function interfaces with examples
What are the Function interface in java8?
Java8 introduced a functional interface to achieve functional programming with java language.
java.util.function package has many predefined functional interfaces.
These functional interfaces can be assigned as a target to lambda expressions or method /constructor references.
These will be very useful during an iteration of objects, stream API processing, and manipulate like filter data to process numeric data.
This post is about various numeric(Integer, Long, Double related) functional interfaces examples.
ToLongBiFunction interface in java example
This takes two arguments as input produces an output of long type.
The below code takes two long values, added this value using the lambda expression, finally called applyAsLong on the function to return the long result.
import java.util.function.ToLongBiFunction;
public class ToLongBiFunctionExample {
public static void main(String[] args) {
ToLongBiFunction<Long,Long> addLongFunction=(value1, value2)-> value1+value2;
System.out.println(addLongFunction.applyAsLong(50l,40l)); // 90
}
}
ToLongFunction Interface example in java
This function accepts two long values as input and produces a long value.
import java.util.ArrayList;
import java.util.List;
import java.util.function.ToLongFunction;
public class MyToLongFunctionExample {
public static void main(String[] args) {
// program example 1
List<String> list = new ArrayList<String>();
list.add("123");
list.add("43");
list.add("56");
list.add("53");
long[] result = list.stream().mapToLong(element -> Long.parseLong(element)).toArray();
System.out.println(result);
// program example 2
ToLongFunction<String> longFunction = value -> Long.parseLong(value);
System.out.println(longFunction.applyAsLong("123"));
}
}
In the above code, Two examples are presented.
- The first example is Converting a list of Strings into Long.
- Create a List of Strings
- Using streams interface, Iterated each element with mapToLong method
- mapToLong(ToLongFunction f) used
ToLongFunctionindirectly using the lambda expression. - Next example, converting string data to the long object using ToLongFunction.
ToDoubleFunction Interface java example
It is functional represents to produce double value from the object.
import java.util.function.ToDoubleFunction;
public class MyToDoubleFunctionExample {
public static void main(String[] args) {
ToDoubleFunction<String> doubleFunction = value -> Double.parseDouble(value);
System.out.println(doubleFunction.applyAsDouble("54")); // output 54
}
}
ToDoubleBiFunction interface example
This function takes two parameters as input produces a double value.
The below code takes two integer values, added these values using a lambda expression, and assigned them to the functional interface. finally, called applyAsDouble on the function to return the Double Result.
import java.util.function.ToDoubleBiFunction;
public class Test {
public static void main(String[] args) {
ToDoubleBiFunction<Integer,Integer> addDoubleFunction=(value1, value2)-> value1+value2;
System.out.println(addDoubleFunction.applyAsDouble(5,4)); //outputs 9
}
}
ToIntFunction example
It takes one object data and produces an Integer value.
It is an example of converting a string to an Integer using the ToInitFunction interface and lambda expressions.
import java.util.function.ToIntFunction;
public class Test {
public static void main(String[] args) {
ToIntFunction<String> intFunction= value-> Integer.parseInt(value);
System.out.println(intFunction.applyAsInt("5")); // output 5
}
}
ToIntBiFunction interface java
This interface takes two arguments of integer and produces an integer result.
import java.util.fnction.DoubleToLongFunction;
import java.util.function.ToIntBiFunction;
public class Test {
public static void main(String[] args) {
ToIntBiFunction<Integer,Integer> addIntFunction=(value1, value2)-> value1+value2;
System.out.println(addIntFunction.applyAsInt(15,14)); //outputs 29
}
}
DoubleToLongFunction
This interface takes the Double value and produces the result as a long value.
import java.util.function.DoubleToLongFunction;
public class Test {
public static void main(String[] args) {
DoubleToLongFunction function = value-> new Double(value).longValue();
System.out.println(function.applyAsLong(43.7)); //outputs 43
}
}
IntFunction example
This interface takes the Integer value as an argument and produces a result of the data type given.
import java.util.function.IntFunction;
public class Test {
public static void main(String[] args) {
IntFunction<String> function= value-> String.valueOf(value);
System.out.println(function.apply(43)); // outputs "43"
System.out.println(function.apply(43).length()); // outputs 2
}
}
IntToLongFunction interface
This takes an integer as input and produces a Long value
import java.util.function.IntToLongFunction;
public class Test {
public static void main(String[] args) {
IntToLongFunction fucntion= (value) -> Long.valueOf(value);
System.out.println(fucntion.applyAsLong(21));
}
}
LongFunction Interface
These interfaces accept long value as an argument and produce a result of the data type given.
import java.util.function.LongFunction;
public class Test {
public static void main(String[] args) {
LongFunction<String> longFunction= value-> String.valueOf(value);
System.out.println(longFunction.apply(512l)); // outputs "512"
System.out.println(longFunction.apply(513l).length()); // outputs 3
}
}
LongToDoubleFunction interface
This interface accepts a long value as input and produces a double value as output.
import java.util.function.LongToDoubleFunction;
public class Test {
public static void main(String[] args) {
LongToDoubleFunction longTDoubleFunction= (value) -> Double.valueOf(value);
System.out.println(longTDoubleFunction.applyAsDouble(21l)); //outputs 21.0
}
}
LongToIntFunction interface
This interface takes long as input and outputs int value
import java.util.function.LongToIntFunction;
public class Test {
public static void main(String[] args) {
LongToIntFunction longToIntFunction = value-> (int)(value);
System.out.println(longToIntFunction.applyAsInt(20143)); // outputs 20143
}
}
