How do I use the LongBinaryOperator functional interface in Java?

The LongBinaryOperator is a functional interface in Java that is part of the java.util.function package, introduced in Java 8. It represents an operation upon two long values that produces a single long result. This can be thought of as a primitive specialization of the BinaryOperator interface for long types.

Key Features of LongBinaryOperator:

  • It’s a functional interface, so it can be used with a lambda expression or method reference.
  • It has a single abstract method, applyAsLong, which takes two long arguments and returns a long.

Functional Method:

long applyAsLong(long left, long right);

The two parameters (left and right) represent the two long values on which the operation will be performed, and the result is also a long.


Usage

Here are a few examples of how to use the LongBinaryOperator:

Example 1: Using a Lambda Expression

We can define a LongBinaryOperator using a lambda expression:

package org.kodejava.util.function;

import java.util.function.LongBinaryOperator;

public class LongBinaryOperatorExample {
    public static void main(String[] args) {
        // Example: sum of two long values
        LongBinaryOperator sumOperator = (a, b) -> a + b;

        long result = sumOperator.applyAsLong(10L, 20L);
        // Output: Result: 30
        System.out.println("Result: " + result);
    }
}

Example 2: Using a Method Reference

If there’s an existing method that matches the signature of applyAsLong(long, long), we can use a method reference instead of a lambda:

package org.kodejava.util.function;

import java.util.function.LongBinaryOperator;

public class LongBinaryOperatorExample2 {
    public static void main(String[] args) {
        // Example: method reference for multiplying two numbers
        LongBinaryOperator multiplyOperator = Math::multiplyExact;

        long result = multiplyOperator.applyAsLong(10L, 20L);
        // Output: Result: 200
        System.out.println("Result: " + result);
    }
}

Example 3: Custom Implementation

We can also implement the interface explicitly, though this is less common since lambdas are more concise:

package org.kodejava.util.function;

import java.util.function.LongBinaryOperator;

public class LongBinaryOperatorExample3 {
    public static void main(String[] args) {
        LongBinaryOperator customOperator = new LongBinaryOperator() {
            @Override
            public long applyAsLong(long left, long right) {
                // Custom logic: return the larger of the two numbers
                return Math.max(left, right);
            }
        };

        long result = customOperator.applyAsLong(15L, 20L);
        // Output: Result: 20
        System.out.println("Result: " + result);
    }
}

Example 4: Composing with Streams

LongBinaryOperator is often used in combination with streams, such as reducing a series of long values into a single value:

package org.kodejava.util.function;

import java.util.stream.LongStream;
import java.util.function.LongBinaryOperator;

public class LongBinaryOperatorExample4 {
    public static void main(String[] args) {
        LongBinaryOperator maxOperator = Math::max;

        long max = LongStream.of(5L, 10L, 15L, 20L)
                .reduce(0L, maxOperator);

        // Output: Max: 20
        System.out.println("Max: " + max);
    }
}

Summary

The LongBinaryOperator is a simple yet powerful functional interface that allows we to work with long values directly, avoiding boxing and unboxing overheads. It’s mainly useful for scenarios involving two long inputs and one long output, such as mathematical or logical operations. Lambdas, method references, and its integration with streams make it highly versatile and efficient.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.