java8 Numeric Object Consumer examples | Functional Interface tutorials
Learn the Numeric BinaryOperators
class in java8 with examples.
Primitive Consumer functional interfaces are defined in java.util.function
package.
It has only a single abstract method that takes object values and other numeric values and results in nothing.
It accepts two values as input like the BiConsumer
interface.
All functional interfaces use as a variable assigned with lambda expression or method reference.
You can check my related posts:
ObjIntConsumer class in java8
java.util.function.ObjIntConsumer
is a functional interface.
It has a single abstract method. accept(T t, integer value) takes two arguments t is an object and value is an integer
ObjIntConsumer Method Reference example
Method reference is declared and called with a double colon
operator.
It is assigned with Consumer
, calling the accept()
method with two arguments, and returns nothing.
import java.util.function.ObjIntConsumer;
public class MethodReferenceExample {
static void append(String str, int value) {
System.out.println(str+value);
}
public static void main(String[] args) {
ObjIntConsumer<String> IntToDoubleFunction = MethodReferenceExample::append;
IntToDoubleFunction.accept("Hello",4);
}
}
The output of the above generated code is
Hello4
ObjIntConsumer Lambda Expression Example
Here lambda expression is declared by passing two parameters and assigned with the ObjIntConsumer
interface.
Calling accepts with Object and int value and returns nothing.
Here is an example for ObjIntConsumer Lambda Expression
import java.util.function.ObjDoubleConsumer;
public class Test {
public static void main(String[] args) throws Exception{
ObjDoubleConsumer <String> objDoubleConsumer = ( v1 , v2 ) -> { System.out.println(v1 + " " +v2);};
objDoubleConsumer.accept("object",1d);
}
}
Output:
test 1
ObjLongConsumer interface in java
java.util.function.ObjLongConsumer
is a functional interface.
It has a single abstract method - accept(Object, primitive value)
, Takes object and integer value as input, and returns no result.
import java.util.function.ObjLongConsumer;
public class Test {
public static void main(String[] args) throws Exception{
ObjLongConsumer <String> objLongConsumer = ( v1 , v2 ) -> { System.out.println(v1 + " " +v2);};
objLongConsumer.accept("object",4l);
}
}
The output of the above code execution is
object 4
Conclusion
Learn Object Primitive Consumers with examples using lambda expression and method reference syntax.