Thursday, August 16, 2018

Java8 - Learn Numeric Function interfaces with examples

Java8 features-  Function interface examples


java8 features - Numeric Functionl interfaces with examples

Java8 introduced a functional interface to achieve functional programming with java language. java.util.function package has many predefined functional interfaces. This 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

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
ToLongBiFunction<Long,Long> addLongFunction=(value1,value2)-> value1+value2;
System.out.println(addLongFunction.applyAsLong(50l,40l)); // 90

ToLongFunction Interface 

This function accepts any value and produces 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 presented.
 The first example is Converting a list of Strings into Long Create a List of Strings Using streams interface, Iterated each element ith mapToLong method mapToLong(ToLongFunction f) used ToLongFunction indirectly using the lambda expression.
Next example converted string data to the long object using ToLongFunction.

ToDoubleFunction Interface  

This 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 

This function takes two parameters as input produces double value. The below code takes two integer values, added this values using lambda expression and assigned to the functional interface, finally called applyAsDouble on the function to return the Double Result.
ToDoubleBiFunction<Integer,Integer> addDoubleFunction=(value1,value2)-> value1+value2;
System.out.println(addDoubleFunction.applyAsDouble(5,4)); //outputs 9

ToIntFunction example

 takes one object data and produces Integer value. This is an example of converting a string to Integer using ToInitFunction interface and lambda expressions
ToIntFunction<String> intFunction=value-> Integer.parseInt(value);
System.out.println(intFunction.applyAsInt("5")); // output 5

ToIntBiFunction<T,U> interface 

This interface takes two arguments and produces an integer result
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 result as long value

DoubleToLongFunction function = value-> new Double(value).longValue();
System.out.println(function.applyAsLong(43.7)); //outputs 43

IntFunction example 

This interfaces takes Integer value as arguments and produces a result of data type given

   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 Long value
IntToLongFunction fucntion= (value) -> Long.valueOf(value);
System.out.println(fucntion.applyAsLong(21));

LongFunction Interface 

This interfaces accept long value as an argument and produces a result of data type given

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 long value as input and produces double value as output
LongToDoubleFunction longTDoubleFunction= (value) -> Double.valueOf(value);
System.out.println(longTDoubleFunction.applyAsDouble(21l)); //outputs 21.0

LongToIntFunction interface

 This interface takes long and outputs int value
LongToIntFunction longToIntFunction = value-> (int)(value);
System.out.println(longToIntFunction.applyAsInt(20143)); // outputs 20143

Related article


EmoticonEmoticon