GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java IntBinaryOperator Interface - Complete Tutorial with Examples Java IntBinaryOperator InterfaceLast modified: April 16, 2025
The
IntBinaryOperator Interface Overview
@FunctionalInterface
public interface IntBinaryOperator {
int applyAsInt(int left, int right);
}
The code above shows the simple structure of Basic IntBinaryOperator UsageThe simplest way to use IntBinaryOperator is with lambda expressions. We define how to combine two int values into one result. The example shows addition. Main.java
package com.zetcode;
import java.util.function.IntBinaryOperator;
public class Main {
public static void main(String[] args) {
// Define an addition operator
IntBinaryOperator add = (a, b) -> a + b;
// Apply the operator
System.out.println("5 + 3 = " + add.applyAsInt(5, 3));
System.out.println("10 + 20 = " + add.applyAsInt(10, 20));
// Define a multiplication operator
IntBinaryOperator multiply = (x, y) -> x * y;
System.out.println("7 * 6 = " + multiply.applyAsInt(7, 6));
}
}
This example demonstrates basic IntBinaryOperator usage with lambda expressions. We create two operators: one for addition and one for multiplication. Each operator takes two int values and returns their combined result. Using IntBinaryOperator with reduce
Main.java
package com.zetcode;
import java.util.Arrays;
import java.util.function.IntBinaryOperator;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// Define a sum operator
IntBinaryOperator sumOperator = (a, b) -> a + b;
// Reduce the array to a single sum
int sum = Arrays.stream(numbers)
.reduce(0, sumOperator);
System.out.println("Sum of numbers: " + sum);
// Define a max operator
IntBinaryOperator maxOperator = (a, b) -> a > b ? a : b;
// Find maximum value
int max = Arrays.stream(numbers)
.reduce(Integer.MIN_VALUE, maxOperator);
System.out.println("Max value: " + max);
}
}
This example shows IntBinaryOperator used with stream reduction. We define two operators: one for summing values and one for finding the maximum. The reduce operation applies the operator cumulatively to all elements. Custom Mathematical OperationsWe can implement more complex mathematical operations using IntBinaryOperator. This example demonstrates power calculation and GCD (Greatest Common Divisor). Main.java
package com.zetcode;
import java.util.function.IntBinaryOperator;
public class Main {
public static void main(String[] args) {
// Power calculation
IntBinaryOperator power = (base, exponent) -> {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
};
System.out.println("2^5 = " + power.applyAsInt(2, 5));
// GCD calculation using Euclidean algorithm
IntBinaryOperator gcd = (a, b) -> {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
};
System.out.println("GCD of 54 and 24: " + gcd.applyAsInt(54, 24));
}
}
This example implements two custom mathematical operations. The power operator calculates base raised to exponent. The GCD operator implements the Euclidean algorithm. Both show how IntBinaryOperator can encapsulate complex logic. Combining with Other Functional InterfacesIntBinaryOperator can be combined with other functional interfaces to create more powerful operations. This example shows combination with IntPredicate. Main.java
package com.zetcode;
import java.util.function.IntBinaryOperator;
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
// Conditional sum - only add if both numbers are even
IntBinaryOperator conditionalSum = (a, b) -> {
IntPredicate isEven = n -> n % 2 == 0;
return isEven.test(a) && isEven.test(b) ? a + b : 0;
};
System.out.println("Sum if even (4,6): " +
conditionalSum.applyAsInt(4, 6));
System.out.println("Sum if even (3,8): " +
conditionalSum.applyAsInt(3, 8));
// Safe division - avoid division by zero
IntBinaryOperator safeDivide = (a, b) -> {
return b != 0 ? a / b : 0;
};
System.out.println("10 / 2 = " + safeDivide.applyAsInt(10, 2));
System.out.println("10 / 0 = " + safeDivide.applyAsInt(10, 0));
}
}
This example demonstrates combining IntBinaryOperator with other functional interfaces. The conditionalSum only adds numbers if both are even. The safeDivide operator handles division by zero gracefully. Factory Methods for Common OperationsWe can create factory methods that return common IntBinaryOperator instances. This approach promotes code reuse and follows the factory pattern. Main.java
package com.zetcode;
import java.util.function.IntBinaryOperator;
public class Main {
public static IntBinaryOperator createAdder(int increment) {
return (a, b) -> a + b + increment;
}
public static IntBinaryOperator createMultiplier(int factor) {
return (a, b) -> a * b * factor;
}
public static void main(String[] args) {
// Create customized operators
IntBinaryOperator addWithBonus = createAdder(5);
IntBinaryOperator multiplyWithFactor = createMultiplier(2);
System.out.println("Add with bonus: " +
addWithBonus.applyAsInt(10, 20));
System.out.println("Multiply with factor: " +
multiplyWithFactor.applyAsInt(3, 4));
// Compose operations
int result = addWithBonus.andThen(r -> r * 2)
.applyAsInt(10, 20);
System.out.println("Composed result: " + result);
}
}
This example shows factory methods creating customized IntBinaryOperator instances. The createAdder method returns an operator that adds a bonus. The createMultiplier includes an extra factor. We also demonstrate chaining. Using in Custom AccumulatorIntBinaryOperator can be used to implement custom accumulation logic. This example creates a simple statistics calculator that processes arrays. Main.java
package com.zetcode;
import java.util.function.IntBinaryOperator;
public class Main {
public static int processArray(int[] array, int initial,
IntBinaryOperator operator) {
int result = initial;
for (int value : array) {
result = operator.applyAsInt(result, value);
}
return result;
}
public static void main(String[] args) {
int[] data = {12, 34, 56, 78, 90};
// Calculate sum
int sum = processArray(data, 0, (a, b) -> a + b);
System.out.println("Sum: " + sum);
// Calculate product
int product = processArray(data, 1, (a, b) -> a * b);
System.out.println("Product: " + product);
// Find minimum
int min = processArray(data, Integer.MAX_VALUE,
(a, b) -> a < b ? a : b);
System.out.println("Minimum: " + min);
}
}
This example demonstrates using IntBinaryOperator in a custom accumulator. The processArray method applies the operator to all elements. We reuse the same method for different operations by passing different operators. SourceJava IntBinaryOperator Interface Documentation In this article, we've covered the essential methods and features of the Java IntBinaryOperator interface. Understanding these concepts is crucial for functional programming with primitive types in Java applications. AuthorMy name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts. List all Java tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |