binaryoperator java_Java 8 BinaryOperator Examples

In Java 8, BinaryOperator is a functional interface and it extends BiFunction.

The BinaryOperator takes two arguments of the same type and returns a result of the same type of its arguments.

BinaryOperator.java

@FunctionalInterface

public interface BinaryOperator extends BiFunction {

}

The BiFunction takes two arguments of any type, and returns a result of any type.

BiFunction.java

@FunctionalInterface

public interface BiFunction {

R apply(T t, U u);

}

1. BinaryOperator

1.1 In this example, the BiFunction which accepts and returns the same type, can be replaced with BinaryOperator.

Java8BinaryOperator1.java

package com.mkyong;

import java.util.function.BiFunction;

import java.util.function.BinaryOperator;

public class Java8BinaryOperator1 {

public static void main(String[] args) {

// BiFunction

BiFunction func = (x1, x2) -> x1 + x2;

Integer result = func.apply(2, 3);

System.out.println(result); // 5

// BinaryOperator

BinaryOperator func2 = (x1, x2) -> x1 + x2;

Integer result2 = func.apply(2, 3);

System.out.println(result2); // 5

}

}

Output

5

5

2. BinaryOperator as argument

2.1 This example simulates a stream.reduce() to sum all the Integer.

Java8BinaryOperator2.java

package com.mkyong;

import java.util.Arrays;

import java.util.List;

import java.util.function.BinaryOperator;

public class Java8BinaryOperator2 {

public static void main(String[] args) {

Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Integer result = math(Arrays.asList(numbers), 0, (a, b) -> a + b);

System.out.println(result); // 55

Integer result2 = math(Arrays.asList(numbers), 0, Integer::sum);

System.out.println(result2); // 55

}

public static T math(List list, T init, BinaryOperator accumulator) {

T result = init;

for (T t : list) {

result = accumulator.apply(result, t);

}

return result;

}

}

Output

55

55

3. IntBinaryOperator

3.1 If the math operations involve primitive types like int, change to IntBinaryOperator for better performance.

Java8BinaryOperator3.java

package com.mkyong;

import java.util.function.IntBinaryOperator;

import java.util.stream.IntStream;

public class Java8BinaryOperator3 {

public static void main(String[] args) {

int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int result = math((numbers), 0, (a, b) -> a + b);

System.out.println(result); // 55

int result2 = math((numbers), 0, Integer::sum);

System.out.println(result2); // 55

IntStream

}

public static int math(int[] list, int init, IntBinaryOperator accumulator) {

int result = init;

for (int t : list) {

result = accumulator.applyAsInt(result, t);

}

return result;

}

}

Output

55

55

4. BinaryOperator.maxBy() and BinaryOperator.minBy()

4.1 This example uses BinaryOperator and a custom Comparator to find the highest and lowest pay developer from a list of developers.

Java8BinaryOperator4.java

package com.mkyong;

import java.math.BigDecimal;

import java.util.Arrays;

import java.util.Comparator;

import java.util.List;

import java.util.function.BinaryOperator;

public class Java8BinaryOperator4 {

public static void main(String[] args) {

Developer dev1 = new Developer("jordan", BigDecimal.valueOf(9999));

Developer dev2 = new Developer("jack", BigDecimal.valueOf(8888));

Developer dev3 = new Developer("jaden", BigDecimal.valueOf(10000));

Developer dev4 = new Developer("ali", BigDecimal.valueOf(2000));

Developer dev5 = new Developer("mkyong", BigDecimal.valueOf(1));

List list = Arrays.asList(dev1, dev2, dev3, dev4, dev5);

// 1. Create a Comparator

Comparator comparing = Comparator.comparing(Developer::getSalary);

// 2. BinaryOperator with a custom Comparator

BinaryOperator bo = BinaryOperator.maxBy(comparing);

Developer result = find(list, bo);

System.out.println(result); // Developer{name='jaden', salary=10000}

// one line

// find developer with highest pay

Developer developer = find(list, BinaryOperator.maxBy(Comparator.comparing(Developer::getSalary)));

System.out.println(developer); // Developer{name='jaden', salary=10000}

// find developer with lowest pay

Developer developer2 = find(list, BinaryOperator.minBy(Comparator.comparing(Developer::getSalary)));

System.out.println(developer2); // Developer{name='mkyong', salary=1}

}

public static Developer find(List list, BinaryOperator accumulator) {

Developer result = null;

for (Developer t : list) {

if (result == null) {

result = t;

} else {

result = accumulator.apply(result, t);

}

}

return result;

}

}

Developer.java

package com.mkyong;

import java.math.BigDecimal;

public class Developer {

String name;

BigDecimal salary;

public Developer(String name, BigDecimal salary) {

this.name = name;

this.salary = salary;

}

//...

}

Output

Developer{name='jaden', salary=10000}

Developer{name='jaden', salary=10000}

Developer{name='mkyong', salary=1}

References

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值