Close

Java 8 Functional Interfaces - BiFunction.andThen() Examples

Java 8 Functional Interfaces Java Java API 


Interface:

java.util.function.BiFunction

java.util.function.BiFunctionBiFunctionLogicBig

Method:

default <V> BiFunction<T,U,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.

In other words the 'after' function maps the return value of this BiFunction<T, U, R> from type R to type V.


Examples


package com.logicbig.example.bifunction;

import java.math.BigDecimal;
import java.util.function.BiFunction;
import java.util.function.Function;

public class AndThenExample {

public static void main(String... args) {
BiFunction<Double, String, Long> adderToLong = (d, s) ->
(long) (d + Long.parseLong(s));

Function<Long, BigDecimal> bigDecimalConverter = l -> BigDecimal.valueOf(l);

BiFunction<Double, String, BigDecimal> biFunction = adderToLong
.andThen(bigDecimalConverter);

BigDecimal bd = biFunction.apply(20.33d, "34");
System.out.println(bd);
}
}

Output

54




package com.logicbig.example.bifunction;

import java.util.function.BiFunction;

public class AndThenExample2 {

public static void main(String... args) {
Double d = ((BiFunction<String, String, Integer>) String::indexOf)
.andThen(Integer::doubleValue)
.apply("banana", "nan");
System.out.println(d);
}
}

Output

2.0




package com.logicbig.example.bifunction;

import java.util.function.BiFunction;

public class AndThenExample3 {

public static void main(String... args) {
Double d = ((BiFunction<Integer, Integer, Long>) Math::multiplyFull)
.andThen(Math::sqrt)
.apply(3, 4);
System.out.println(d);
}
}

Output

3.4641016151377544




See Also