This articles provide good examples of all functional interfaces with TWO method arguments from java.util.function package. It covers all methods in interfaces.
Functional Interface | Both Method Arguments | Return |
java.util.function.BiConsumer | Any type | No return |
java.util.function.BiFunction | Any type | Any Type |
java.util.function.BinaryOperator | Both Same type | Same type as method arguments |
java.util.function.BiPredicate | Any type | boolean (Primitive) |
java.util.function.DoubleBinaryOperator | double (Primitive) | double (Primitive) |
java.util.function.IntBinaryOperator | int (Primitive) | int (Primitive) |
java.util.function.LongBinaryOperator | long (Primitive) | long (Primitive) |
java.util.function.BiConsumer Examples
- Method arguments = 2 arguments of any type
- Method Return = No return value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* * Printing currency amount. Both arguments are unchanged. Just appended & * printed. */ BiConsumer<String, Integer> currencyPrinter = (s, a) -> System.out.println("Amount is " + s + a); currencyPrinter.accept("$", 100); currencyPrinter.accept("€", 100); /* * Appending strings. First argument is used as a state & second arguments kept * on appending */ BiConsumer<StringBuilder, String> appender = (a, b) -> a.append(b); StringBuilder greeting = new StringBuilder("H"); appender.accept(greeting, "E"); appender.accept(greeting, "L"); appender.accept(greeting, "L"); appender.accept(greeting, "O"); System.out.println(greeting); |
1 2 3 |
Amount is $100 Amount is €100 HELLO |
java.util.function.BiFunction Examples
- Method arguments = 2 arguments of any type
- Method Return = Return value of any type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/* * Total price = number of items (n) * price of single item (p) */ BiFunction<Integer, Float, Double> totalPriceCalculator = (n, p) -> (double) n * p; Double totalPrice = totalPriceCalculator.apply(10, 50.50f); System.out.println("Total price is $" + totalPrice); /* * Composing new function using java.util.function.BiFunction & * java.util.function.Function. * * Discount of $10.30 */ Function<Double, Double> discountCalculator = r -> r - 10.30; BiFunction<Integer, Float, Double> finalPriceCalculator = totalPriceCalculator.andThen(discountCalculator); Double finalPrice = finalPriceCalculator.apply(20, 25.50f); System.out.println("Final price is $" + finalPrice); |
1 2 |
Total price is $505.0 Final price is $499.7 |
java.util.function.BinaryOperator Examples
- Method arguments = 2 arguments of exact same type
- Method Return = Return value of exact same type as that of method arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/* * BinaryOperator area of rectangle using Area = length * width. * * Length, Width & Area all will be Integer i.e same type. */ BinaryOperator<Integer> rectangleAreaCalculator = (l, w) -> l * w; Integer area = rectangleAreaCalculator.apply(3, 5); System.out.println("Area is " + area); /* * Get maximum value using BinaryOperator.maxBy & natural order comparator */ BinaryOperator<Double> maxFetcher = BinaryOperator.maxBy(Comparator.naturalOrder()); Double max = maxFetcher.apply(30.1, 50.32); System.out.println("Maximum is " + max); /* * Get minimum value using BinaryOperator.minBy & natural order comparator */ BinaryOperator<Double> minFetcher = BinaryOperator.minBy(Comparator.naturalOrder()); Double min = minFetcher.apply(30.1, 50.32); System.out.println("Minimum is " + min); |
1 2 3 |
Area is 15 Maximum is 50.32 Minimum is 30.1 |
java.util.function.BiPredicate Examples
- Method arguments = 2 arguments of any type
- Method Return = boolean (Primitive)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* * Check if String number is same as Integer number. */ BiPredicate<String, Integer> isTextEqualsIntegerPredicate = (s, i) -> Integer.valueOf(s).equals(i); boolean isTextEqualsInteger = isTextEqualsIntegerPredicate.test("100", 100); System.out.println("Text number equals Integer Number = " + isTextEqualsInteger); /* * Check if text number & Integer number are different by just negating earlier * BiPredicate */ BiPredicate<String, Integer> isTextDifferentThanIntegerPredicate = isTextEqualsIntegerPredicate.negate(); boolean isTextDifferentThanInteger = isTextDifferentThanIntegerPredicate.test("999", 100); System.out.println("Text number & Integer Number are different = " + isTextDifferentThanInteger); /* * Use BiPredicate.or() to compose new predicate which is OR of two different * predicate. */ BiPredicate<String, Integer> isTextGreaterThanIntegerPredicate = (s, i) -> Integer.valueOf(s).compareTo(i) > 0; BiPredicate<String, Integer> isTextGreaterThanAndEqualsIntegerPredicate = isTextGreaterThanIntegerPredicate .or(isTextEqualsIntegerPredicate); System.out.println( "Is greater than or equals to = " + isTextGreaterThanAndEqualsIntegerPredicate.test("150", 100)); System.out.println( "Is greater than or equals to = " + isTextGreaterThanAndEqualsIntegerPredicate.test("100", 100)); |
1 2 3 4 |
Text number equals Integer Number = true Text number & Integer Number are different = true Is greater than or equals to = true Is greater than or equals to = true |
java.util.function.DoubleBinaryOperator Examples
- Method arguments = 2 arguments of double (Primitive) type
- Method Return = double (Primitive)
1 2 3 4 5 |
DoubleBinaryOperator areaInDouble = (l, w) -> l * w; double widthDouble = 20; double lengthDouble = 30; double areaDouble = areaInDouble.applyAsDouble(lengthDouble, widthDouble); System.out.println("Area in double type is " + areaDouble); |
1 |
Area in double type is 600.0 |
java.util.function.IntBinaryOperator Examples
- Method arguments = 2 arguments of int (Primitive) type
- Method Return = int (Primitive)
1 2 3 4 5 |
IntBinaryOperator areaInInt = (l, w) -> l * w; int widthInt = 20; int lengthInt = 30; int areaInt = areaInInt.applyAsInt(lengthInt, widthInt); System.out.println("Area in int type is " + areaInt); |
1 |
Area in int type is 600 |
java.util.function.LongBinaryOperator Examples
- Method arguments = 2 arguments of long (Primitive) type
- Method Return = dlong (Primitive)
1 2 3 4 5 |
LongBinaryOperator areaInLong = (l, w) -> l * w; long widthLong = 20; long lengthLong = 30; long areaLong = areaInLong.applyAsLong(lengthLong, widthLong); System.out.println("Area in long type is " + areaLong); |
1 |
Area in long type is 600 |