ObjDoubleConsumer 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 a single an object-valued and a double-valued argument as an input and produces no output. In this post, we are going to see several implementations of ObjDoubleConsumer Interface by using different examples.
ObjDoubleConsumer Interface in Java 8 with Examples...!!! Click To Tweet
Look at ObjDoubleConsumer Javadoc description below:
ObjDoubleConsumer Interface contains following method:
accept
Let’s discuss this method:
accept
This method performs operation on the given arguments and return no result.
1 | void accept(T t, double value); |
Below is an example to demonstrate accept() method:
Example 1. with Integer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.util.function.ObjDoubleConsumer; public class ObjDoubleConsumerInterfaceJava8Example1 { public static void main(String args[]) { System.out.println("Ex. 1 - ObjDoubleConsumer - Integer example \n"); ObjDoubleConsumer <Integer> objIntConsumberObj = (t, value) -> System.out.println(t * value); objIntConsumberObj.accept(5, 10); } } |
Example 2. with String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.function.ObjDoubleConsumer; public class ObjDoubleConsumerInterfaceJava8Example2 { public static void main(String args[]) { System.out.println("Ex. 2 - ObjDoubleConsumer - String example \n"); ObjDoubleConsumer <String> objDoubleConsumberObj = (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."); } }; objDoubleConsumberObj.accept("justanordinarywriter.com", 15.0); objDoubleConsumberObj.accept("techndeck.com", 15.0); } } |
Example 3. with ArrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.ArrayList; import java.util.List; import java.util.function.ObjDoubleConsumer; public class ObjDoubleConsumerInterfaceJava8Example3 { public static void main(String args[]) { System.out.println("Ex. 3 - ObjDoubleConsumer - list example \n"); ObjDoubleConsumer < List < Double >> objDoubleConsumberObj = (t, value) - > { if (t.contains(value)) { System.out.println("Expected value is present in the string"); } else { System.out.println("Expected value is not present in the string"); } }; List < Double > listObj = new ArrayList < Double > (); listObj.add(25.0); listObj.add(100.4577); listObj.add(73.77777777777); objDoubleConsumberObj.accept(listObj, 100.4577); objDoubleConsumberObj.accept(listObj, 200); } } |
Example 4. with Custom Class Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package org.personal.samples; import java.util.function.ObjDoubleConsumer; public class ObjDoubleConsumerInterfaceJava8Example4 { public static void main(String args[]) { System.out.println("Ex. 4 - ObjDoubleConsumer - Custom class obj example \n"); ObjDoubleConsumer < Agent > ObjDoubleConsumberObj = (t, value) - > { if (t.getId() == value) { System.out.println("\nPerson with Id: " + t.getId() + " exist." + "\nHis/Her name is: " + t.getName()); } else { System.out.println("\nPerson with Id: " + t.getId() + " does not exist."); } }; Agent p1 = new Agent(25.5555, "Isha Durani"); Agent p2 = new Agent(10.1000, "Gaurangee"); ObjDoubleConsumberObj.accept(p1, 5475.0055); ObjDoubleConsumberObj.accept(p2, 10.1000); } } class Agent { double id; String name; public double getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } Agent(double id, String name) { this.id = id; this.name = name; } } |
ObjDoubleConsumer Interface in Java 8 with Examples...!!! Click To Tweet
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