Close

Java 8 Functional Interfaces - BiFunction.apply() Examples

Java 8 Functional Interfaces Java Java API 


Interface:

java.util.function.BiFunction

java.util.function.BiFunctionBiFunctionLogicBig

Method:

R apply(T t, U u)

Applies this function to the given arguments.


Examples


package com.logicbig.example.bifunction;

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

public class ApplyExample {

public static void main(String... args) {
BiFunction<Integer, Number, String> integerAdder = (integer, number) -> {
return Double.toString(number.doubleValue() + integer);
};

String s = integerAdder.apply(10, new BigDecimal("1.1"));
System.out.println(s);

}
}

Output

11.1




package com.logicbig.example.bifunction;

import java.util.function.BiFunction;

public class ApplyExample2 {

public static void main(String... args) {
BiFunction<Integer, Number, String> integerAdder = ApplyExample2::addIntegerToANumber;
String s = integerAdder.apply(11, Double.valueOf(1.9));
System.out.println(s);
}

private static String addIntegerToANumber(Integer integer, Number number) {
return Double.toString(number.doubleValue() + integer);
}
}

Output

12.9




If we have a static method which takes two arguments of types A and B and returns a type R, then it's reference can be assigned to BiFunction<A,B,R>

package com.logicbig.example.bifunction;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

public class ApplyExample3 {

public static void main(String... args) {
List<Integer> list = List.of(1, 4, 5, 8, 11);
List<Integer> compareList = getAnotherList(list, 5, Integer::compare);
System.out.println(compareList);

List<String> unsignedStrings = getAnotherList(list, 2, Integer::toUnsignedString);
System.out.println(unsignedStrings);

List<Integer> exacts = getAnotherList(list, 3, Math::addExact);
System.out.println(exacts);

List<Long> multiples = getAnotherList(list, 2, Math::multiplyFull);
System.out.println(multiples);
}

public static <T, U, R> List<R> getAnotherList(List<T> list, U userObject,
BiFunction<T, U, R> biFunction) {
List<R> returnList = new ArrayList<>();
list.forEach(t -> returnList.add(biFunction.apply(t, userObject)));
return returnList;
}
}

Output

[-1, -1, 0, 1, 1]
[1, 100, 101, 1000, 1011]
[4, 7, 8, 11, 14]
[2, 8, 10, 16, 22]




If we have a member method in class T, which takes one argument of types A and returns a type R, then it's reference can be assigned to BiFunction<T,A,R>

package com.logicbig.example.bifunction;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

public class ApplyExample4 {

public static void main(String... args) {
List<Integer> list = List.of(1, 4, 5, 8, 11);
List<Boolean> equals = getAnotherList(list, 5, Integer::equals);
System.out.println(equals);

List<String> fruits = List.of("Apple", "Banana", "Orange");
List<String> substrings = getAnotherList(fruits, 2, String::substring);
System.out.println(substrings);
List<Integer> indexes = getAnotherList(fruits, "a", String::indexOf);
System.out.println(indexes);
}

public static <T, U, R> List<R> getAnotherList(List<T> list, U userObject,
BiFunction<T, U, R> biFunction) {
List<R> returnList = new ArrayList<>();
list.forEach(t -> returnList.add(biFunction.apply(t, userObject)));
return returnList;
}
}

Output

[false, false, true, false, false]
[ple, nana, ange]
[-1, 1, 2]

If we have a reference t of the class T which has a method M, the method M takes two argument of types A and B, and returns type R, then BiFunction<A,B,R> => t::M

package com.logicbig.example.bifunction;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

public class ApplyExample5 {

public static void main(String... args) {

String fruit = "banana";

List<Integer> list = List.of(1, 2, 3);
//using reference of String#substring(int beginIndex, int endIndex)
List<String> subStrings = getAnotherList(list, 4, fruit::substring);
System.out.println(subStrings);

List<String> searchList = List.of("a", "b", "n");
//using reference of String#indexOf(String str, int fromIndex)
List<Integer> indexes = getAnotherList(searchList, 2, fruit::indexOf);
System.out.println(indexes);
}

public static <T, U, R> List<R> getAnotherList(List<T> list, U userObject,
BiFunction<T, U, R> biFunction) {
List<R> returnList = new ArrayList<>();
list.forEach(t -> returnList.add(biFunction.apply(t, userObject)));
return returnList;
}
}

Output

[ana, na, a]
[3, -1, 2]




See Also