Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaLongConsumer Interface in Java with Examples

LongConsumer Interface in Java with Examples

The LongConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one long-valued argument but does not return any value. The lambda expression assigned to an object of LongConsumer type is used to define its accept() which eventually applies the given operation on its only argument. It is similar to using an object of type Consumer<Long> The LongConsumer interface consists of the following two functions:

accept()

This method accepts one value and performs the operation on its only argument. Syntax:

void accept(long value)

Parameters: This method takes in only one parameter:

  • value– the input argument

Returns: This method does not return any value. Below is the code to illustrate accept() method: 

Java




import java.util.function.LongConsumer;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Create a LongConsumer Instance
        LongConsumer
            display
            = a -> System.out.println(a * 100);
 
        // Using accept() method
        display.accept(3);
    }
}


Output:

300

andThen()

It returns a composed LongConsumer wherein the parameterized LongConsumer will be executed after the first one. If the evaluation of either operation throws an error, it is relayed to the caller of the composed operation. Note: The operation being passed as the argument should be of type LongConsumer. Syntax:

default LongConsumer andThen(LongConsumer after)

Parameters: This method accepts a parameter after which is the LongConsumer to be applied after the current one. Return Value: This method returns a composed LongConsumer that first applies the current operation first and then the after operation. Exception: This method throws NullPointerException if the after operation is null. Below programs illustrate andThen() method: Program 1: 

Java




import java.util.function.LongConsumer;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Create a LongConsumer Instance
        LongConsumer
            display
            = a -> System.out.println(a * 10);
        LongConsumer mul = a -> 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 -> a *= 10;
            LongConsumer composite = mul.andThen(null);
            composite.accept(3);
        }
        catch (Exception e) {
            System.out.println("Exception : " + 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 -> a = a / (a - 3);
            LongConsumer mul = a -> a *= 10;
            LongConsumer composite = mul.andThen(divide);
            composite.accept(3);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}


Output:

Exception : java.lang.ArithmeticException: / by zero
RELATED ARTICLES

Most Popular

Recent Comments