LongConsumer Interface in Java with Examples

Last Updated : 18 Jun, 2026

The LongConsumer interface is a specialized functional interface available in the java.util.function package, introduced in Java 8. It represents an operation that accepts a single primitive long value as input and performs an action without returning any result

  • Primitive specialization of Consumer<Long>.
  • Avoids boxing and unboxing overhead.
  • Supports lambda expressions and method references.
Java
import java.util.function.LongConsumer;

public class Main {
    public static void main(String[] args) {

        LongConsumer square =
            num -> System.out.println(num * num);

        square.accept(5);
    }
}

Output
25

Explanation: In this example, a LongConsumer accepts a long value and prints its square. The accept() method passes the value 5 to the LongConsumer, which performs the operation without returning any value.

Syntax

@FunctionalInterface
public interface LongConsumer

Methods of LongConsumer Interface

1. accept() Method

The accept() method is the functional method of the LongConsumer interface. It accepts a single long value and performs the specified operation on it without returning any result.

  • Does not return any value.
  • Performs side-effect operations.

Syntax:

void accept(long value)

Parameters: value-> The input long value.
Return Value: Does not return any value.

Java
import java.util.function.LongConsumer;

public class GFG {
    public static void main(String args[])
    {

        // Create a LongConsumer Instance
        LongConsumer
            display
            = a -&gt; System.out.println(a * 100);

        // Using accept() method
        display.accept(3);
    }
}

Output
300

Explanation: In this example, a LongConsumer is created using a lambda expression that multiplies the input value by 100 and prints the result. The accept() method passes the value 3, producing the output 300.

2. andThen() Method

The andThen() method returns a composed LongConsumer that performs the current operation first and then executes another specified LongConsumer.

  • Chains multiple operations together.
  • Executes operations sequentially.

Syntax:

default LongConsumer andThen(LongConsumer after)

Parameters after- The operation to execute after the current operation
Return Value: Returns a composed LongConsumer.
Exception: Throws NullPointerException if after is null.

Java
import java.util.function.LongConsumer;

public class GFG {
    public static void main(String args[])
    {

        // Create a LongConsumer Instance
        LongConsumer
            display
            = a -&gt; System.out.println(a * 10);
        LongConsumer mul = a -&gt; a *= 100;

        // Using addThen() method
        LongConsumer composite = mul.andThen(display);

        composite.accept(3);
    }
}

Output
30

Program 2: To demonstrate when NullPointerException is returned. 

Java
import java.util.function.LongConsumer;

public class GFG {
    public static void main(String args[])
    {

        try {

            LongConsumer mul = a -&gt; a *= 10;
            LongConsumer composite = mul.andThen(null);
            composite.accept(3);
        }
        catch (Exception e) {
            System.out.println(&quot;Exception : &quot; + e);
        }
    }
}

Output
Exception : java.lang.NullPointerException

Program 3: To demonstrate how an Exception in the after function is returned and handled. 

Java
import java.util.function.LongConsumer;

public class GFG {
    public static void main(String args[])
    {

        try {
            LongConsumer divide = a -&gt; a = a / (a - 3);
            LongConsumer mul = a -&gt; a *= 10;
            LongConsumer composite = mul.andThen(divide);
            composite.accept(3);
        }
        catch (Exception e) {
            System.out.println(&quot;Exception : &quot; + e);
        }
    }
}

Output
Exception : java.lang.ArithmeticException: / by zero
Comment