# Java 8 Functional Interfaces - BinaryOperator.apply() Examples

## Method:

```R apply(T t1, T t2)
```

Applies this function to the given arguments.

## Examples

`package com.logicbig.example.binaryoperator;import java.util.function.BinaryOperator;public class ApplyExample {    public static void main(String... args) {        BinaryOperator<Integer> sumOfSquare = (a, b) -> a * a + b * b;        Integer i = sumOfSquare.apply(3, 4);        System.out.println(i);    }} `

#### Output

`25`

`package com.logicbig.example.binaryoperator;import java.util.function.BinaryOperator;public class ApplyExample2 {    public static void main(String... args) {        BinaryOperator<Integer> sumOfSquare = ApplyExample2::getSquareSum;        Integer i = sumOfSquare.apply(3, 4);        System.out.println(i);    }    private static Integer getSquareSum(Integer a, Integer b) {        return a * a + b * b;    }} `

#### Output

`25`

`package com.logicbig.example.binaryoperator;import java.math.BigDecimal;import java.util.List;import java.util.function.BinaryOperator;import java.util.stream.Collectors;public class ApplyExample3 {    public static void main(String... args) {        List<Double> doubles = List.of(1d, 3d, 5d, 7d);        //static method reference        List<Double> list = transformList(doubles, Math::pow, 3d);        System.out.println(list);        //instance method reference        List<BigDecimal> bigDecimals = List.of(BigDecimal.valueOf(1), BigDecimal.valueOf(3),                BigDecimal.valueOf(5), BigDecimal.valueOf(7));        List<BigDecimal> list2 = transformList(bigDecimals, BigDecimal::add, BigDecimal.TEN);        System.out.println(list2);        list2 = transformList(bigDecimals, BigDecimal::multiply, BigDecimal.TEN);        System.out.println(list2);        //instance method reference of a particular instance        List<String> cars = List.of("cadillac", "chrysler", "ferrari");        String s = "I drive a ferrari car";        List<String> stringList = transformList(cars, s::replaceAll, "ford");        System.out.println(stringList);    }    public static <T> List<T> transformList(List<T> list,BinaryOperator<T> binaryOperator,  T userObject) {        return list.stream()                   .map(e -> binaryOperator.apply(e, userObject))                   .collect(Collectors.toList());    }} `

#### Output

`[1.0, 27.0, 125.0, 343.0][11, 13, 15, 17][10, 30, 50, 70][I drive a ferrari car, I drive a ferrari car, I drive a ford car]`