ObjLongConsumer Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expects an object-valued and a long-valued argument as input but produces no output. In this post, we are going to see several implementations of ObjLongConsumer Interface by using different examples.

ObjLongConsumer Interface in Java 8 with Examples...!!! Share on X
Look at ObjLongConsumer Javadoc description below:

ObjLongConsumer Interface contains following method:
accept
Let’s discuss this method:
accept
This method performs operation on the given argument and return no result.
void accept(T t, long value);
Below is an example to demonstrate accept() method:
Example 1. with Integer
import java.util.function.ObjLongConsumer;
public class ObjLongConsumerInterfaceJava8Example1 {
public static void main(String args[]) {
System.out.println("Ex. 1 - ObjLongConsumer - Integer example \n");
ObjLongConsumer <Integer> objLongConsumberObj = (t, value) -> System.out.println(t * value);
objLongConsumberObj.accept(5, 10 L);
}
}
Example 2. with String
import java.util.function.ObjLongConsumer;
public class ObjLongConsumerInterfaceJava8Example2 {
public static void main(String args[]) {
System.out.println("Ex. 2 - ObjLongConsumer - String example \n");
ObjLongConsumer <String> ObjLongConsumberObj = (t, value) -> {
if (t.length() > value) {
System.out.println("String is bigger than the expected value.");
} else if (t.length() == value) {
System.out.println("String lenght is equal to expected value.");
} else {
System.out.println("String is shorter than the expected value.");
}
};
ObjLongConsumberObj.accept("justanordinarywriter.com", 15 L);
ObjLongConsumberObj.accept("techndeck.com", (long) 15);
}
}
Java 8 ObjLongConsumer Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.
ObjLongConsumer Interface in Java 8 with Examples...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8